Search code examples
c#.net.net-coreasp.net-core-webapiserilog

Serilog implementaiton breaking other configuration


Implementing serilog into our project. We already have AspNetCoreRateLimit implemented to thorttle calls to the API. The project is a .Net Core project but build targeting Net461 due to some dependencies.

Prior to serilog the api was fine.

Now having installed serilog we are getting an error.

The error bubbles up from calling this which previously worked until serilog is introduced.

services.Configure<IpRateLimitOptions>(_configuration.GetSection("IpRateLimiting"));

Startup() has:

Log.Logger = new LoggerConfiguration()
          .WriteTo.File("Logs/FSCPAPI-{Date}.log")
          .CreateLogger();

The below is in Configure()

loggerfactory.AddSerilog();

The error coming from services.Configure<>() is:

System.TypeLoadException occurred HResult=0x80131522 Message=Method 'get_Name' in type 'Microsoft.Extensions.Options.ConfigurationChangeTokenSource`1' from assembly 'Microsoft.Extensions.Options.ConfigurationExtensions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation. Source=Microsoft.Extensions.Options.ConfigurationExtensions StackTrace: at Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure[TOptions](IServiceCollection services, IConfiguration config) at NGB.IFS.PurchApp.Services.Startup.ConfigureServices(IServiceCollection services) in C:\Users\saynort\Documents\Repos\ngb.ifs.purchapp\ngb.ifs.purchapp\NGB.IFS.PurchApp.Services\Startup.cs:line 86

I have serilog, serilog.extensions.logging, and serilog.sinks.file Nuget packages installed.


Solution

  • From the Serilog.Extensions.Logging Github project:

    ASP.NET Core 2.0 applications should prefer Serilog.AspNetCore and UseSerilog() instead.

    Remove the Serilog and Serilog.Extensions.Logging packages. Then, install the Serilog.AspNetCore package using:

    PM> Install-Package Serilog.AspNetCore -DependencyVersion Highest