[英]Serilog File Sink - System.MissingMethodException: Method not found: Serilog.LoggerConfiguration
I'm getting a MissingMethodException when I try to use the File Sink.当我尝试使用文件接收器时,我收到了 MissingMethodException。
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day, shared: true)
.CreateLogger();
This is really weird as I've used it before with the same settings and it never gave any issue, but I'm sure this is what's giving the error because if I get rid of the .WriteTo.File() line I don't get the error.这真的很奇怪,因为我以前用相同的设置使用过它,它从来没有出现过任何问题,但我确定这是导致错误的原因,因为如果我去掉 .WriteTo.File() 行,我就不会'得到错误。
Here's my packages.config:这是我的packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ModPlus.Revit.API.2020" version="1.0.0" targetFramework="net472" />
<package id="Serilog" version="2.9.0" targetFramework="net472" />
<package id="Serilog.Sinks.File" version="4.1.0" targetFramework="net472" />
</packages>
Can you think of a reason why this is happening?你能想出发生这种情况的原因吗?
I've seen similar issue and it is usually due to version mismatch in assembly files but I checked and I'm referencing the right versions (at least as far as I can see in my packages.config )我见过类似的问题,通常是由于程序集文件中的版本不匹配,但我检查了并引用了正确的版本(至少就我在packages.config 中看到的而言)
EDIT: Here's the full unhandled exception编辑:这是完整的未处理异常
EDIT 2: The code that calls Serilog is a plugin for Revit (3D modelling software) and I just tried to run this plugin in 2 different versions of the platform:编辑 2:调用 Serilog 的代码是 Revit(3D 建模软件)的插件,我只是尝试在 2 个不同版本的平台上运行此插件:
Both the versions of the software use the same version of .NET Framework, 4.7.2两个版本的软件都使用相同版本的 .NET Framework,4.7.2
Does this give more clues?这是否提供了更多线索? What could be going wrong in one version and not on the other?在一个版本中会出现什么问题,而在另一个版本中不会出现什么问题?
This error suggests that somehow you're getting an old version of Serilog.Sinks.File.dll
in your output folder, which is not the version you've installed via NuGet.此错误表明您在输出文件夹中以某种方式获得了旧版本的Serilog.Sinks.File.dll
,这不是您通过 NuGet 安装的版本。
Open your .csproj
file and check any references to Serilog.Sinks.File.dll
and make sure you have only one reference, and that it's pointing to the correct nuget package folder (4.1.0).打开您的.csproj
文件并检查对Serilog.Sinks.File.dll
任何引用,并确保您只有一个引用,并且它指向正确的 nuget 包文件夹 (4.1.0)。
It should look something like:它应该看起来像:
<ItemGroup>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.2.9.0\lib\net46\Serilog.dll</HintPath>
</Reference>
<Reference Include="Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Sinks.File.4.1.0\lib\net45\Serilog.Sinks.File.dll</HintPath>
</Reference>
</ItemGroup>
You can also use something like dotPeek to inspect the assemblies in the output folder and confirm if you have the right ones.您还可以使用dotPeek 之类的工具来检查输出文件夹中的程序集并确认您是否拥有正确的程序集。
Also, although I don't think it's related to this particular error, make sure you have auto-generate binding redirects enabled in your project properties, so that Serilog.Sinks.File
's reference to Serilog 2.5.0 gets redirected to Serilog 2.9.0.此外,虽然我认为这与此特定错误无关,但请确保在项目属性中启用了自动生成绑定重定向,以便将Serilog.Sinks.File
对 Serilog 2.5.0 的引用重定向到 Serilog 2.9 .0。
Now that you've updated the question with new information:现在您已经用新信息更新了问题:
The code that calls Serilog is a plugin for Revit (3D modelling software) and I just tried to run this plugin in 2 different versions of the platform:调用 Serilog 的代码是 Revit(3D 建模软件)的插件,我只是尝试在 2 个不同版本的平台上运行这个插件:
- Revit 2020, I get that unhandled exeption Revit 2020,我得到了未处理的例外
- Revit 2019, it works fine! Revit 2019,它运行良好!
The MissingMethodException
suggests that the Serilog.Sinks.File.dll
loaded in the AppDomain doesn't have the extension method .WriteTo.File(path, rollingInterval, shared)
in it. MissingMethodException
表明 AppDomain 中加载的Serilog.Sinks.File.dll
中没有扩展方法.WriteTo.File(path, rollingInterval, shared)
。
That method was introduced in Serilog.Sinks.File.dll
v4.0.0 which tells me that Revit 2000 is loading an older version of Serilog.Sinks.File.dll
( v3.2.0 or older, before that method existed).该方法是在Serilog.Sinks.File.dll
v4.0.0中引入的,它告诉我 Revit 2000 正在加载旧版本的Serilog.Sinks.File.dll
( v3.2.0或更早版本,在该方法存在之前)。
I can think of two possibilities for that to happen:我可以想到发生这种情况的两种可能性:
Revit 2000 itself uses Serilog
and Serilog.Sinks.File
for logging, and it uses an old version of the sink Revit 2000 本身使用Serilog
和Serilog.Sinks.File
进行日志记录,它使用的是旧版本的 sink
Another plugin (that is being loaded before yours) uses Serilog
and Serilog.Sinks.File
for logging, and they use an old version of the sink另一个插件(在你的之前加载)使用Serilog
和Serilog.Sinks.File
进行日志记录,它们使用旧版本的接收器
Things you could try:你可以尝试的事情:
Find out which version is being loaded, and downgrade your plugin to use that version instead找出正在加载的版本,并降级您的插件以使用该版本
Find where the old Serilog.Sinks.File.dll
is being loaded from (physical path), and replace it with the newer version that you want to use in your plugin - and test that it doesn't break whoever is using the old version.找到从哪里加载旧的Serilog.Sinks.File.dll
(物理路径),并将其替换为您要在插件中使用的较新版本 - 并测试它不会破坏使用旧版本的任何人. If you go this route, you might want to replace Serilog.dll
too to make sure it's the version you want.如果你走这条路,你可能也想替换Serilog.dll
以确保它是你想要的版本。
You can use Fuslogvw.exe (Assembly Binding Log Viewer) to inspect where assemblies are being loaded from.您可以使用Fuslogvw.exe(程序集绑定日志查看器)来检查程序集的加载位置。
Alternatively, you can iterate over AppDomain.CurrentDomain.GetAssemblies()
when your plugin loads, and inspect the CodeBase
to see the location where Serilog.Sinks.File.dll
is being loaded from.或者,您可以在加载插件时遍历AppDomain.CurrentDomain.GetAssemblies()
,并检查CodeBase
以查看加载Serilog.Sinks.File.dll
的位置。
NB: On a regular application, the answer would be adding binding redirects to the config file of the host app, but unfortunately binding redirection is not a possibility for you given that Serilog assembly versions are all locked at version 2.0.0.0 so .NET can't tell the difference between the assemblies in the v4.0.0 and v3.2.0 nuget packages...注意:在常规应用程序中,答案是将绑定重定向添加到主机应用程序的配置文件中,但不幸的是,鉴于Serilog 程序集版本都锁定在 2.0.0.0 版,因此无法进行绑定重定向,因此 .NET 可以不区分v4.0.0和v3.2.0 nuget 包中的程序集...
PS: You might want to ask this on the Autodesk Revit forum . PS:您可能想在Autodesk Revit 论坛上提问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.