缺少方法异常 Serilog.Context.LogContext.Push

MissingMethodException Serilog.Context.LogContext.Push

我在我的解决方案中做了一些 consolidation 的 NuGet 包,现在我收到:

异常信息:

MissingMethodException: Method not found: 'System.IDisposable Serilog.Context.LogContext.Push(Serilog.Core.ILogEventEnricher)'.

异常调用堆栈:

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

当我这样打电话时:

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

package.config

<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 引用,但我没有看到它。

我也整理了一个full repro of the problem here.

Serilog.Enrichers.ClientInfo 包括 Serilog v2.4.0.0,一个旧版本及其 NuGet 包:

我在 GitHub 项目上打开了 an issue

如您所知,问题出在软件包 Serilog.Enrichers.ClientInfo including an old version of Serilog.dll within the NuGet package, which is wrong (as it's supposed to be a dependency via NuGet only), thus the long-term fix is the maintainer fix that as you reported

一个短期修复方法是手动编辑您的 .csproj 文件以使用 Serilog 包中的正确 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 project is pointing to the Serilog.dll that is inside of the Serilog.Enrichers.ClientInfo 包裹:

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

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


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