使用serilog关闭ASP.NET Core 2.2中的MVC请求日志记录 [英] Turn off mvc request logging in asp.net core 2.2 with serilog

查看:404
本文介绍了使用serilog关闭ASP.NET Core 2.2中的MVC请求日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Serilog.Extensions.Logging.File登录文件.

这是我的appsettings.json文件:

  "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"
    }
  }

我的Startup.cs代码:

My Startup.cs code:

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

但是它仍然记录如下的mvc请求信息:

But It still log mvc request info such as below:

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

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]请求在202.16ms 200(791a596a)中完成

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]请求启动HTTP/1.1 GET http://localhost:53534/favicon.ico (ca22a1cb)

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]请求在73.5631ms 200(791a596a)中完成

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

推荐答案

将以下代码行添加到启动程序的构造函数中

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

有关更多信息: Serilog

您可以从应用设置中删除配置.

You can remove configuration from appsettings.

这篇关于使用serilog关闭ASP.NET Core 2.2中的MVC请求日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆