我将默认日志记录功能与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
}
答案 0 :(得分: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);
如果您只想禁用接收警告级别MVC的日志消息,您应该能够使用"Microsoft.AspNetCore.Mvc": LogLevel.Warning
等文件管理器
答案 1 :(得分:0)
请务必拉入Microsoft.Extensions.Logging.Filter nuget包。我引入了Microsoft.Extensions.Logging nuget,但我不知道我还需要Microsoft.Extensions.Logging.Filter nuget。