加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何降低aspnetcore中mvc管道的日志级别?

发布时间:2020-12-15 23:40:29 所属栏目:百科 来源:网络整理
导读:因此,在ASPNETCORE 2.0项目中,我向日志工厂添加了一个日志记录提供程序(serilog)以及一个控制台接收器.它工作得很好,但我注意到所有框架请求管道(如http请求响应)都将每个小细节记录为INFO. [17:37:26 INF] Request starting HTTP/1.1 GET http://localhost:
因此,在ASPNETCORE 2.0项目中,我向日志工厂添加了一个日志记录提供程序(serilog)以及一个控制台接收器.它工作得很好,但我注意到所有框架请求管道(如http请求响应)都将每个小细节记录为INFO.

[17:37:26 INF] Request starting HTTP/1.1 GET http://localhost:5000/test/health
[17:37:26 INF] Executing action method DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) with arguments (null) - ModelState is Valid
[17:37:26 INF] Health check called.
[17:37:27 INF] Executing ObjectResult,writing value Microsoft.AspNetCore.Mvc.ControllerContext.
[17:37:27 INF] Executed action DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) in 203.8825ms
[17:37:27 INF] Request finished in 343.9801ms 200 application/json; charset=utf-8
[17:38:07 INF] Request starting HTTP/1.1 GET http://localhost:5000/test/health
[17:38:07 INF] Executing action method DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) with arguments (null) - ModelState is Valid
[17:38:07 INF] Health check called.
[17:38:07 INF] Executing ObjectResult,writing value Microsoft.AspNetCore.Mvc.ControllerContext.
[17:38:07 INF] Executed action DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) in 53.5876ms
[17:38:07 INF] Request finished in 60.2195ms 200 application/json; charset=utf-8

Info是我们用于生产日志记录的最小日志级别,我们有一个Loggly接收器,其限制为1GB /天,所以我觉得MVC记录所有请求信息,因为INFO有点令人讨厌,我想将它降低到DEBUG.为了清楚起见,我不想提高我的日志记录级别以防止INFO到达接收器,我希望.Net将其日志级别从INFO降低到DEBUG.

这可能吗?怎么样?

解决方法

你需要使用 log filtering.

You can specify a minimum log level for a specific provider and category or for all providers or all categories. Any logs below the minimum level aren’t passed to that provider,so they don’t get displayed or stored.

过滤规则可以通过配置或代码定义.它在代码中的外观示例:

// using Serilog.Extensions.Logging;

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(logging => 
            {
                logging.AddFilter<SerilogLoggerProvider>("Microsoft",LogLevel.Warning);
            }
            .UseStartup<Startup>()
            .Build();

请注意,在这种情况下,AddFilter仅适用于Serilog日志提供程序,因为我们指定了提供程序类型.如果要为所有提供程序定义筛选器,请使用:

logging.AddFilter(“Microsoft”,LogLevel.Warning);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读