3

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?

Andrea Tassera
  • 359
  • 3
  • 17
  • You'll need to post the full exception details, including stack trace, to narrow this down – Nicholas Blumhardt Oct 18 '20 at 22:04
  • Thanks @NicholasBlumhardt ! Edited my original post to add the screenshot of the unhandled exeption – Andrea Tassera Oct 19 '20 at 06:02
  • I used the exact same snippet as you with same package versions and it worked fine. Have you tried deleting the bin and obj folder and then building the project? – Harsh Oct 19 '20 at 06:23
  • Hi @Harsh yeah I tried that too but didn't work. Same error. It's very weird because I'm using Serilog with the file sink in another project and it works fine there. – Andrea Tassera Oct 19 '20 at 08:26

2 Answers2

1

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

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • Thanks for the reply! I checked csproj, dotPeek, versions of the dlls in the packages folder and auto-generate property and it all matches what you're showing, but still it isn't working... – Andrea Tassera Oct 20 '20 at 22:30
  • @AndreaTassera You mentioned you checked the dlls in the packages folder - what about the output folders i.e. `bin\Debug`, `bin\Release` – C. Augusto Proiete Oct 21 '20 at 04:24
  • Can you reproduce the same error in other machines? My next guess is that something is off with your .NET install, or Visual Studio, or something else on the machine. You can try to repair / reinstall these... – C. Augusto Proiete Oct 21 '20 at 04:26
  • Yes, I checked the dlls in the bin folders too and they match. But from your second comment I actually had a suspicion and I tried something else. I just edited the original post with EDIT 2. That might give more clues? – Andrea Tassera Oct 21 '20 at 06:47
  • @AndreaTassera See if [my new answer](https://stackoverflow.com/a/64470372/211672) helps – C. Augusto Proiete Oct 21 '20 at 19:37
  • Some referenced package can internally reference other version of serilog and that might cause lower version to be automatically chosen, those packages might be not possible to replace with ones with corrent serilog version, this change was unfortunate. – Jaroslav Daníček Feb 06 '22 at 10:32
1

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.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • I tried it now and it somehow fixed itself o.O I haven't changed anything but it now works. Revit is a very strange beast and sometimes does this kind of things... – Andrea Tassera Oct 22 '20 at 02:25