[英]NLog 5 ignores Logging.LogLevel.Default option
我正在嘗試使用以下 appsettings.json 為.NET 6 控制台應用程序配置 NLog:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
},
"NLog": {
"targets": {
"console": {
"type": "Console",
"layout": "${longdate} ${pad:padding=5:inner=${uppercase:${level}}} ${message}${onexception:inner=${newline}${exception:format=ToString}}"
}
},
"rules": [
{
"logger": "*",
"writeTo": "console",
"minLevel": "Trace"
}
]
}
}
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, logging) =>
{
var configuration = context.Configuration.GetSection("NLog");
if (!configuration.Exists())
return;
LogManager.Configuration = new NLogLoggingConfiguration(configuration);
logging.ClearProviders();
logging.AddNLog();
})
.ConfigureServices(services =>
{
services.AddHostedService<MyHostedService>();
})
.Build()
.Run();
MyHostedService
的以下代碼按預期與NLog.Extensions.Logging 1.7.5一起工作 - 只有Information
級別和更高級別的消息才會顯示在控制台中。
private readonly ILogger<MyHostedService> _logger;
public MyHostedService(ILogger<MyHostedService> logger)
{
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogTrace("Test");
_logger.LogDebug("Test");
_logger.LogInformation("Test");
_logger.LogWarning("Test");
_logger.LogError("Test");
_logger.LogCritical("Test");
return Task.CompletedTask;
}
但如果安裝了5.2.0 ,則它會輸出所有消息。 我究竟做錯了什么? NLog.Extensions.Logging 5.2.0 package 是否需要額外的設置來考慮Logging.LogLevel.Default
配置選項值?
NLog v5 包括幾個重大更改,其中一個是NLog.Extensions.Logging 沒有任何過濾器
你可以這樣做:
"NLog": {
"targets": {
"console": {
"type": "Console",
"layout": "${longdate} ${pad:padding=5:inner=${uppercase:${level}}} ${message}${onexception:inner=${newline}${exception:format=ToString}}"
}
},
"rules": [
{
"logger": "*",
"finalMinLevel": "Info"
},
{
"logger": "Microsoft*",
"finalMinLevel": "Warn"
},
{
"logger": "*",
"writeTo": "console",
"minLevel": "Trace"
}
]
}
或者你可以這樣做:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
},
"NLog": {
"RemoveLoggerFactoryFilter": false
}
},
"NLog": {
"targets": {
"console": {
"type": "Console"
}
},
"rules": [
{
"logger": "*",
"writeTo": "console",
"minLevel": "Trace"
}
]
}
}
另見: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.