gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:37:43 25 4
gpt4 key购买 nike

因此,在一个 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 有点令人讨厌,我想降低它调试。明确地说,我不想提高我的日志记录级别以防止 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);

关于c# - 如何降低 aspnetcore 中 mvc 管道的日志级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com