logging - 如何在记录器中抑制 MVC 信息消息?

标签 logging asp.net-core asp.net-core-mvc

我将默认日志记录功能与 serilog 结合使用,将日志记录信息写入文件。它有效,但我不知道如何抑制所有信息性消息(主要是 MVC)。我尝试了很多选项,添加了 Logging.Filter 但没有结果......我正在使用 VS2015 和 IIS-Express。

Logger output: 
2016-06-16 19:34:52.984 +02:00 [Information] HttpContext.User merged via     AutomaticAuthentication from authenticationScheme: "Identity.Application".
2016-06-16 19:34:52.993 +02:00 [Information] Executing action method     "Test.Controllers.SurveysController.Create (Test)" with arguments (["Test.Survey"]) -     ModelState is Invalid'
2016-06-16 19:34:53.027 +02:00 [Error] this is the error I want to show in     "SurveysController"
System.DivideByZeroException: Attempted to divide by zero.
   at Test.Controllers.SurveysController.<Create>d__7.MoveNext() in     C:\Users\dummy\Documents\Visual Studio     2015\Projects\Test\src\Test\Controllers\SurveysController.cs:line 97
2016-06-16 19:34:53.061 +02:00 [Information] Executing ViewResult, running view at     path "/Views/Surveys/Create.cshtml".
2016-06-16 19:34:53.083 +02:00 [Information] Executed action     "Test.Controllers.SurveysController.Create (Test)" in 94.1372ms
2016-06-16 19:34:53.087 +02:00 [Information] Request finished in 111.506ms 200     text/html; charset=utf-8

Startup.cs:
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,     ILoggerFactory loggerFactory)
        {
            loggerFactory.AddSerilog();
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug(LogLevel.Debug);

            //extra package installed to try and filter logging
            loggerFactory.WithFilter(new FilterLoggerSettings
            {
                { "Microsoft", LogLevel.Warning },
                { "System", LogLevel.Warning },
                { "Test", LogLevel.Information }
            });

appsettings.json:
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Error",       // information => error
      "System": "Error",        // information => error
      "Microsoft": "Error"      // information => error
    }

最佳答案

问题是 WithFilter 扩展返回一个包装的记录器工厂,您需要在其上添加 SerilogConsole 等提供程序生效。

例子:

    //extra package installed to try and filter logging
    loggerFactory = loggerFactory.WithFilter(new FilterLoggerSettings
    {
        { "Microsoft", LogLevel.Warning },
        { "System", LogLevel.Warning },
        { "Test", LogLevel.Information }
    });

    loggerFactory.AddSerilog();
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug(LogLevel.Debug);

如果您只想禁用接收警告级别 MVC 的日志消息,您应该能够使用类似 "Microsoft.AspNetCore.Mvc": LogLevel.Warning

的文件管理器

关于logging - 如何在记录器中抑制 MVC 信息消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37866163/

相关文章:

c# - 如何在 MVC 中使用 Nlog 将消息记录到 txt 文件?

python - 使用正则表达式读取日志

asp.net-core - 在 ASP.NET Core 5 中的 razor View 中使用字形图标

java - 堆栈跟踪在日志文件中的一行上打印所有内容 (Java)

Java - 配置自定义记录器以供使用

c# - "the term ' 添加迁移 ' is not recognized as the name of a cmdlet" Visual Studio 2019

docker - 无法使用 Nginx 在 Blazor docker compose 配置中转发真实客户端 IP 地址

asp.net - ASP.NET Core 中的 OAuth 授权服务

asp.net - ASP.NET vNext MVC 6 Web Api 的帮助页面

asp.net-core-mvc - 错误请求 - 从 LAN 访问本地主机 Web API 或 Web 应用程序时主机名无效