Serilog - How to stop request life cycle logs and enable only custom Information logs

1.7k views Asked by At

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 ?

1

There are 1 answers

0
Allie On

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.