如何配置环境以防止记录Microsoft消息?

时间:2018-01-31 18:04:55

标签: logging .net-core amazon-ecs

我有一个运行为AWS ECS服务的Microsoft DotNet Core 2.0应用程序,它位于负载均衡器后面,其Cloudwatch日志充满了来自AspNetCore.Hosting层的消息,因为负载均衡器的运行状况检查调用了应用程序的状态端点:

17:49:18 [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
17:49:18 Request starting HTTP/1.1 GET http://10.10.1.184/status
17:49:18 [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
17:49:18 Executing action method Controller.Controllers.StatusController.Status (MyEcsApplication) with arguments ((null)) - ModelState is Valid
17:49:18 [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor[1]
17:49:18 Executing JsonResult, writing value Controller.Models.StatusModel.
17:49:18 [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
17:49:18 Executed action Controller.Controllers.StatusController.Status (MyEcsApplication) in 4.8748ms
17:49:18 [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
17:49:18 Request finished in 32.7132ms 200 application/json; charset=utf-8

应用程序的日志级别设置为Debug,因为我正在进行初始测试,这些消息使日志非常嘈杂。有没有办法配置日志记录以从Microsoft.AspNetCore中排除邮件?

这是我的appsettings.json文件:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "AWS": {
    "Profile": "default",
    "Region": "us-east-1"
  },
  "Log4Net": {
    "ConfigFileRelativePath": "log4net.xml",
    "Repository": "NETCoreRepository"
  },
  "Swagger": {
    "FileName": "MyEcsApplication.xml"
  }
}

Program.cs启动代码:(编辑为添加ConfigureAppConfiguration)

var host = new WebHostBuilder()
            .UseKestrel()  // use default web server on port 80
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                config.AddEnvironmentVariables();
            })
            .ConfigureLogging((hostingContext, logging) =>
             {
                 logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                 logging.AddConsole();
                 logging.AddDebug();
             })
            .Build();

// run the web host and block until host shutdown
host.Run();

1 个答案:

答案 0 :(得分:1)

尝试将Logging LogLevel更改为以下内容:

"Logging": {
  "LogLevel": {
    "Microsoft": "Error",
    "Default": "Debug"
  }
}

以下是有关过滤的一些文档:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x#log-filtering