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.

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you. I will give this a try to see if it's more seamless!
I gave this a go but I'm afraid this solution only works if it's a .Net Core 2 project. This isn't. If I attempt an upgrade I can the following due to build and dependencies: Error I get is "Package Microsoft.AspNetCore.All 2.0.0 is not compatible with net461". This is my target build due to dependencies I have to include (third party).
I must be losing my mind, as I could have sworn you had mentioned using ASP.NET Core 2.0 at some point in your question. Regardless, you're better off upgrading anyways. .NET Core 2.0 implements .NET Standard 2.0, which means that you can use pretty much any .NET Framework library with it now. As a result, there's not really a need any longer to run on the full framework, unless the library you're referencing does some really Windows-specific stuff.
I think some form of hybrid has worked here: Instead of including the .All core dependency. I have AspNetCore, AspNetCore.Mvc.Core and AspNetCore.MVC.Formatters.Json. The end result is Core 2 Nuget packages but no error target net461. I did have to remove services.AddMVC() from the startup however as it threw a "not implemented" exception. I already have services.AddMvcCore() however and with just that everything works and appears to run. Being honest I'm happy working, and logging is also working but unsure what I've taken out and the impact there!
On your comment. I'm afraid I have no choice but to target 461 full fat in order to use a third parties provider. What do you make of this seemingly hybrid approach. Must say I'm kinda confused why this works as the components mentioned are 2.0.0 versions. Just had to remove AddMVC()
|
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(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.