【发布时间】:2020-12-07 23:45:45
【问题描述】:
我有一个小型控制台应用程序。我已启用调试和控制台日志记录。但是,他们似乎没有尊重我的日志级别配置。这是我的设置:
添加配置文件并创建记录器
// global to Program
private static ILogger<Program> _logger;
private const string _configFile = "appsettings.json";
//inside Main
_config = new ConfigurationBuilder()
.AddJsonFile(_configFile, optional: false, reloadOnChange: true)
//.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddDebug();
builder.AddSimpleConsole();
});
_logger = loggerFactory.CreateLogger<Program>();
appconfig.json
"Logging": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning"
},
"Debug": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"FleetLogix.TcaProcessor": "Trace"
}
},
"SimpleConsole": {
"IncludeScopes": "false",
"SingleLine": "false",
"TimestampFormat": "yyyy-MM-dd HH:mm:ss ",
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"FleetLogix.TcaProcessor": "Information"
}
}
},
它当前设置为登录到两个提供程序,调试(Visual Studio 运行窗格)和控制台。这是有效的。 Debug 是为了在 Trace 上记录,但只是在 Info 上记录。控制台正在 Info 中记录,但没有在消息上添加时间戳。
我的配置设置正确吗? (正确的属性名称、正确的深度等) 我应该把它改成什么?
控制台也有可怕的换行符,阅读告诉我你需要分叉 Microsoft.Extensions.Logging.Console 来修复,所以改天。 :/
【问题讨论】:
-
您可以查看Logging 和console log formatter 文档