2

I am using ASP.NET Core 3.1, I have included the nuget for serilog "Serilog.AspNetCore" version 3.2.0.

Configured to log to a file as below:

    Log.Logger = new LoggerConfiguration()
       .MinimumLevel.Information()
       .WriteTo.File("C:\\ProgramData\\MySolution\\MyLog.txt",
          rollOnFileSizeLimit: true,
          fileSizeLimitBytes: 2000,
          retainedFileCountLimit: 5)
       .CreateLogger();

In my code I log using the code Log.Information("Starting up");

When I view the log I get a lot of data regarding the requests which I really don't need to see in the log

Example:

2020-03-18 12:19:27.355 +05:30 [INF] Content root path: C:\R\Path\MyProject.Notification

2020-03-18 12:19:27.486 +05:30 [INF] Request starting HTTP/2.0 GET https://localhost:44360/swagger/index.html

2020-03-18 12:19:27.707 +05:30 [INF] Request finished in 227.3113ms 200 text/html;charset=utf-8

2020-03-18 12:19:27.909 +05:30 [INF] Requeststarting HTTP/2.0 GET https://localhost:44360/swagger/v1/swagger.json

2020-03-18 12:19:28.064 +05:30 [INF] Request finished in 154.5787ms 200 application/json;charset=utf-8

2020-03-18 12:19:41.221 +05:30 [INF] Request starting HTTP/2.0 GET https://localhost:44360/Client/Index

2020-03-18 12:19:41.238 +05:30 [INF] Authorization was successful.

I really don't need all this log but I only want to log the ones which I specify using a Log.Information("log content") in side my controllers action methods.

How can I achieve this ?

sm101
  • 562
  • 3
  • 10
  • 28
  • 3
    https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/ ? – Ruben Bartelink Mar 18 '20 at 11:24
  • @RubenBartelink Thank you Ruben the "MinimumLevel.Override" did the trick. But I had to specify one each for AspNetCore and EntityFrameworkCore. There was a another attribute as "Filter" but I'm still to get the hang of it. – sm101 Mar 19 '20 at 08:00
  • cool - self answer with that, @ me here and you have an upvote from me (after which I will remove this message) – Ruben Bartelink Mar 19 '20 at 10:18

1 Answers1

2

As indicated with the link by Ruben, add the override for AspNetCore as follows:

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)

This will suppress a lot of unwanted messages.

Allie
  • 1,081
  • 1
  • 13
  • 17