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

谁说我不能喝 提交于 2020-08-10 03:32:28

问题


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");

回答1:


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.



来源:https://stackoverflow.com/questions/58038428/turn-off-mvc-request-logging-in-asp-net-core-2-2-with-serilog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!