如何在记录器中压制MVC信息消息?

Ad *_*its 3 logging asp.net-core-mvc asp.net-core

我将默认日志记录功能与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
    }
Run Code Online (Sandbox Code Playgroud)

Kir*_*lla 7

事情是WithFilter扩展返回一个包装的记录器工厂,你需要添加像Serilog,Console等等的提供程序才能生效.

例:

    //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);
Run Code Online (Sandbox Code Playgroud)

如果您只想禁用接收警告级别MVC的日志消息,您应该能够像使用文件管理器一样 "Microsoft.AspNetCore.Mvc": LogLevel.Warning

  • .WithFilter需要另一个包:"Microsoft.Extensions.Logging.Filter":"1.0.0", (2认同)