我将默认日志记录功能与 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
扩展返回一个包装的记录器工厂,您需要在其上添加 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);
如果您只想禁用接收警告级别 MVC 的日志消息,您应该能够使用类似 "Microsoft.AspNetCore.Mvc": LogLevel.Warning
关于logging - 如何在记录器中抑制 MVC 信息消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37866163/