7

I have an Asp Net Core API that targets the .Net Framework 4.6.1. In this project I use Serilog with Microsoft.Extensions.Logging and also Auofac for DI. Using ILogger< T> logger with DI in the controller constructor works perfectlly and I also get logs.

The fun starts when in the API I need to use another project the use Microsoft.Extensions.Logging and Autofac and expects to receive ILogger< T> in some constructors. I've installed the same version of Microsoft.Extensions.Logging in both projects.

I get the fallowing exception:

Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating Microsoft.Extensions.Logging.Logger1[[Microsoft.AspNetCore.Hosting.Internal.WebHost, Microsoft.AspNetCore.Hosting, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> Microsoft.Extensions.Logging.LoggerFactory -> λ:Microsoft.Extensions.Logging.ILoggerProvider[] -> Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider -> Microsoft.Extensions.Options.OptionsMonitor1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> Microsoft.Extensions.Options.OptionsFactory1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] -> λ:Microsoft.Extensions.Options.IConfigureOptions1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]][] -> Microsoft.Extensions.Logging.Console.ConsoleLoggerOptionsSetup -> Microsoft.Extensions.Logging.Configuration.LoggerProviderConfiguration`1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider, Microsoft.Extensions.Logging.Console, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].'

Inner Exception1: DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Logging.Configuration.ILoggerProviderConfigurationFactory)' on type 'LoggerProviderConfiguration`1'.

Inner Exception2: MethodAccessException: Attempt by method 'Microsoft.Extensions.Logging.Configuration.LoggerProviderConfigurationFactory.GetConfiguration(System.Type)' to access method 'Microsoft.Extensions.Logging.ProviderAliasUtilities.GetAlias(System.Type)' failed.

These are the packages for the API:

<PackageReference Include="Autofac" Version="4.9.4" />
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.1" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
    <PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />

And these are the packages from the other project:

 <PackageReference Include="Autofac" Version="4.9.4" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.1" />
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
RSxx
  • 355
  • 1
  • 4
  • 14

2 Answers2

11

Looks like you might have a mismatched Microsoft.Extensions.Logging.Configuration behind the scenes; try adding:

<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.0.1" />

To both projects.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • This worked for me. Make sure you target the main running project (For me it was the web app referencing other two class libraries) – amrswalha Nov 30 '19 at 04:39
4

! do not believe this is Autofac specific. I am not using autofac, but sreilog. This occurred immediately following updating to 3.0.1 for me.

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Logging.Debug

Rolling back Microsoft.Extensions.Logging from v 3.0.1 to 3.0.0 git me operational again. Not sure root cause yet, but that should get you back in business.

If you are using EF Core and went from 3.0.0 to 3.0.1 you will need to roll those back as well to 3.0.0.

Ryan Anderson
  • 937
  • 10
  • 23