Serilog implementaiton breaking other configuration

2

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.

c#
.net
.net-core
asp.net-core-webapi
serilog
asked on Stack Overflow Oct 27, 2017 by Tezza

2 Answers

5

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
answered on Stack Overflow Oct 27, 2017 by Chris Pratt
0

In case you need example of implementing serilog with AspNetCoreRateLimit, I did override and got it working.

public static class RateLimitSerilogExtensions
{
    public static IApplicationBuilder UseCustomIpRateLimiting(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<IpRateLimitMiddlewareCustom>();
    }
} 

public class IpRateLimitMiddlewareCustom : IpRateLimitMiddleware
    {
        private readonly Microsoft.Extensions.Logging.ILogger _logger;
        private readonly IHostEnvironment _env;
        private readonly ILogger _serilogger; 

        public IpRateLimitMiddlewareCustom(RequestDelegate next, IOptions<IpRateLimitOptions> options, IRateLimitCounterStore counterStore, IIpPolicyStore policyStore, Microsoft.Extensions.Logging.ILogger<IpRateLimitMiddleware> logger, IRateLimitConfiguration config,
             IHostEnvironment env, ILogger serLog) : base(next, options, counterStore, policyStore, config, logger)
        {
            _logger = logger;
            _serilogger = serLog;
            _env = env;
          
        }

        protected override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity, RateLimitCounter counter, RateLimitRule rule)
        {
            _serilogger
                  .ForContext("Blocked by rule", rule.Endpoint)
                  .ForContext("TraceIdentifier", httpContext.TraceIdentifier)
                  .ForContext("Quota", rule.Limit + "/" + rule.Period)
                  .ForContext("Exceeded By", counter.Count)
                  .Information("EService limit reached");
           
        }
    }

In startup you have to call this method first, before the method that comes with the nuget package

 app.UseCustomIpRateLimiting();
    app.UseIpRateLimiting();
answered on Stack Overflow Jul 21, 2020 by Pam Splitt

User contributions licensed under CC BY-SA 3.0