首页 > 解决方案 > MissingMethodException Serilog.Context.LogContext.Push

问题描述

我在我的解决方案中对我的 NuGet 包进行了一些整合,现在我收到了:

异常消息:

MissingMethodException:找不到方法:'System.IDisposable Serilog.Context.LogContext.Push(Serilog.Core.ILogEventEnricher)'。

异常调用堆栈:

在 Serilog.Extensions.Logging.SerilogLoggerProvider.BeginScope[T](T state) 在 Microsoft.Extensions.Logging.Logger.BeginScope[TState](TState state)

当我这样打电话时:

using (logger.BeginScope(new Dictionary<string, object>
{
    ["SomeProperty"] = someVariable,
}))
{
    logger.LogInfo("hello world");
}

包配置

<package id="Microsoft.Extensions.Logging" version="2.1.1" targetFramework="net48" />
<package id="Serilog" version="2.10.0" targetFramework="net48" />
<package id="Serilog.Extensions.Logging" version="3.0.1" targetFramework="net48" />

我似乎有一个不正确的 NuGet 引用,但我没有看到它。

我还在这里对问题进行了完整的复制

标签: c#serilogmicrosoft-extensions-logging

解决方案


如您所知,问题在于包Serilog.Enrichers.ClientInfo包括Serilog.dllNuGet 包中的旧版本,这是错误的(因为它应该是仅通过 NuGet 的依赖项),因此长期修复是正如您报告的那样,维护者修复了该问题。

一个短期的解决方法是从 Serilog 包中手动编辑您的.csproj文件以使用正确的 .Serilog.dll

<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
  <HintPath>..\packages\Serilog.2.10.0\lib\net46\Serilog.dll</HintPath>
</Reference>

您的 repro项目指向Serilog.Enrichers.ClientInfoSerilog.dll内的那个:

<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
  <HintPath>..\packages\Serilog.Enrichers.ClientInfo.1.1.2\lib\net452\Serilog.dll</HintPath>
</Reference>

您还可以添加一个构建后步骤,该步骤始终将正确的内容复制Serilog.dll到项目的输出文件夹中。


更新:这已在Serilog.Enrichers.ClientInfo v1.1.3中修复。


推荐阅读