gpt4 book ai didi

c# - Serilog、Microsoft.Extensions.Logging 和 Autofac

转载 作者:行者123 更新时间:2023-11-30 15:13:25 36 4
gpt4 key购买 nike

我有一个面向 .Net Framework 4.6.1 的 Asp Net Core API。在这个项目中,我将 Serilog 与 Microsoft.Extensions.Logging 以及用于 DI 的 Auofac 结合使用。在 Controller 构造函数中使用带有 DI 的 ILogger< T> 记录器工作得很好,我也得到了日志。

当我需要在 API 中使用另一个项目使用 Microsoft.Extensions.Logging 和 Autofac 并期望在某些构造函数中接收 ILogger< T> 时,乐趣就开始了。我在两个项目中安装了相同版本的 Microsoft.Extensions.Logging。

我得到了以下异常:

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.

这些是 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" />

这些是来自其他项目的包:

 <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" />

最佳答案

看起来您可能在幕后有不匹配的 Microsoft.Extensions.Logging.Configuration;尝试添加:

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

两个项目。

关于c# - Serilog、Microsoft.Extensions.Logging 和 Autofac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954066/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com