Serilog File Sink - System.MissingMethodException: Method not found: Serilog.LoggerConfiguration

Viewed 2123

I'm getting a MissingMethodException when I try to use the File Sink.

            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.

Here's my 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)

EDIT: Here's the full unhandled exception UnhandledException

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:

  • Revit 2020, I get that unhandled exeption
  • Revit 2019, it works fine! (I need the plugin to work on both versions)

Both the versions of the software use the same version of .NET Framework, 4.7.2
Does this give more clues? What could be going wrong in one version and not on the other?

2 Answers

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.

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

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.


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.

binding redirects

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:

  • Revit 2020, I get that unhandled exeption
  • Revit 2019, it works fine!

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.

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

I can think of two possibilities for that to happen:

  1. Revit 2000 itself uses Serilog and Serilog.Sinks.File for logging, and it uses an old version of the sink

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


Things you could try:

  1. Find out which version is being loaded, and downgrade your plugin to use that version instead

  2. 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. If you go this route, you might want to replace Serilog.dll too to make sure it's the version you want.

You can use Fuslogvw.exe (Assembly Binding Log Viewer) to inspect where assemblies are being loaded from.

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.

N.B.: 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...

PS: You might want to ask this on the Autodesk Revit forum.

Related