Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off mvc request logging in asp.net core 2.2 with serilog

I am using Serilog.Extensions.Logging.File to logs in file.

Here is my appsettings.json file:

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "System": "None",
      "Microsoft": "None"
    }
  },
  "LoggingFile": {
    "IncludeScopes": false,
    "pathFormat": "C:/logs/APILogs-{Date}.log",
    "LogLevel": {
      "Default": "Trace",
      "System": "None",
      "Microsoft": "None"
    }
  }

My Startup.cs code:

public void Configure(
            IApplicationBuilder app, 
            IHostingEnvironment env, 
            IApiVersionDescriptionProvider provider,
            ILoggerFactory loggerFactory)
        {
            // Removed other codes
            loggerFactory.AddFile(Configuration.GetSection("LoggingFile"));
        }

But It still log mvc request info such as below:

2019-09-21T13:28:59.6337460+05:30 80000019-0004-ff00-b63f-84710c7967bb [INF] Request starting HTTP/1.1 GET http://localhost:53534/api/values (ca22a1cb)

2019-09-21T13:28:59.8309629+05:30 80000019-0004-ff00-b63f-84710c7967bb [INF] Request finished in 202.16ms 200 (791a596a)

2019-09-21T13:29:00.1500727+05:30 8000001a-0004-ff00-b63f-84710c7967bb [INF] Request starting HTTP/1.1 GET http://localhost:53534/favicon.ico (ca22a1cb)

2019-09-21T13:29:00.2020227+05:30 8000001a-0004-ff00-b63f-84710c7967bb [INF] Request finished in 73.5631ms 200 (791a596a)

I don't to log these. It should only log when I want to log, such as in my contrller

_logger.LogInformation("Hello Info");
_logger.LogError("Hello error");
like image 979
Programmer Avatar asked Oct 30 '25 09:10

Programmer


1 Answers

Add following line of code to constructor of startup

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
            .MinimumLevel.Override("System", LogEventLevel.Error)
            .Enrich.FromLogContext()
            .WriteTo.RollingFile("C:/logs/APILogs-{Date}.log")
            .CreateLogger();

And Then In confgure method, Add below line.

loggerFactory.AddSerilog();

For more info: Serilog

You can remove configuration from appsettings.

like image 52
Amit Kumar Avatar answered Nov 02 '25 00:11

Amit Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!