Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog, Microsoft.Extensions.Logging and Autofac

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" />
like image 866
RSxx Avatar asked Nov 20 '19 12:11

RSxx


People also ask

Does Serilog use ILogger?

NET 6 as a logging provider. Let's set up Serilog as Logging Provider in the native logging system in . NET so you can use the Microsoft ILogger interface.

What is the difference between ILogger and ILoggerFactory?

An ILoggerProvider ILogger is a logger that logs to that specific provider. An ILoggerFactory ILogger is a logger that logs to all registered providers.

Why should I use Serilog?

It's designed to work well with others. Serilog is the reference example of a Structured Logging system - one that doesn't just create messages, it easily captures key attributes and data about the context the message happened in.


1 Answers

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.

like image 96
Nicholas Blumhardt Avatar answered Sep 23 '22 03:09

Nicholas Blumhardt