admin管理员组文章数量:1125738
I'm using NLog.Extensions.Logging
v5.3.15 (NLog
v5.3.4) in my .Net 8 Console Application.
I want, that loggers called Microsoft.*
write with minimum Log-level Info
and all the rest of loggers with minimum log-Level Trace
. Both rules should write to the same target (of type File
).
My rules-section (json) looks like the following:
"rules": [
{
"logger": "Microsoft.*",
"enabled": true,
"level": "Info",
"ruleName": "ruleInfo",
"writeTo": "targetFile",
"final": true
},
{
"logger": "*",
"enabled": true,
"minLevel": "Trace",
"ruleName": "ruleTrace",
"writeTo": "targetFile"
}
]
However, this configuration writes all loggers with minimum log-level Trace
.
The NLog internal Log-file (in Debug-level) shows that this Rule-configuration obviously is not the correct one:
2025-01-09 18:41:27.2665 Debug Message Template Auto Format enabled
2025-01-09 18:41:27.3474 Debug Wrapping target 'targetFile' with AsyncTargetWrapper and renaming to 'targetFile_wrapped'
2025-01-09 18:41:27.3474 Debug Adding target NLog.Targets.Wrappers.AsyncTargetWrapper(Name=targetFile)
2025-01-09 18:41:27.3474 Info Registered target NLog.Targets.Wrappers.AsyncTargetWrapper(Name=targetFile)
2025-01-09 18:41:27.3474 Info NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 5.3.4.2778. Product version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b. GlobalAssemblyCache: False
2025-01-09 18:41:27.3474 Debug --- NLog configuration dump ---
2025-01-09 18:41:27.3474 Debug Targets:
2025-01-09 18:41:27.3474 Debug AsyncWrapper_FileTarget(Name=targetFile)
2025-01-09 18:41:27.3474 Debug Rules:
2025-01-09 18:41:27.3474 Debug logNamePattern: (Microsoft.:StartsWith) levels: [ Info ] writeTo: [ targetFile ] final: True
2025-01-09 18:41:27.3474 Debug logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] writeTo: [ targetFile ]
2025-01-09 18:41:27.3474 Debug --- End of NLog configuration dump ---
2025-01-09 18:41:27.3750 Info Validating config: TargetNames=targetFile, ConfigItems=22 ConfigSection=NLog
2025-01-09 18:41:27.3750 Debug Unused target checking is started... Rule Count: 2, Target Count: 1
2025-01-09 18:41:27.3802 Debug Unused target checking is completed. Total Rule Count: 2, Total Target Count: 1, Unused Target Count: 0
2025-01-09 18:41:27.3955 Info Configuration initialized.
2025-01-09 18:41:27.4161 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Extensions.Hosting.Internal.ApplicationLifetime
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Trace] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Debug] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Info] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Warn] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Error] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Fatal] => targetFile
2025-01-09 18:41:27.4238 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Extensions.Hosting.Internal.Host
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Trace] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Debug] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Info] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Warn] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Error] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Fatal] => targetFile
2025-01-09 18:41:27.4238 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Hosting.Lifetime
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Trace] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Debug] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Info] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Warn] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Error] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Fatal] => targetFile
2025-01-09 18:41:27.4573 Debug Targets configured when LogLevel >= Trace for Logger: HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Trace] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Debug] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Info] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Warn] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Error] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Fatal] => targetFile
2025-01-09 18:41:27.4573 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Extensions.Hosting.BackgroundService
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Trace] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Debug] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Info] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Warn] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Error] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Fatal] => targetFile
2025-01-09 18:41:27.4868 Debug FileTarget(Name=targetFile_wrapped): Preparing for new file: 'D:\src-hpg\HPG.Prototypes\HPG.Proto.NLoggerForNetCore\HPG.Proto.NLoggerForNetCore.ConsoleAppHosted\bin\Debug\net8.0\HOT-CP-PF51VH27__2025-01-09T174127Z__pid55280.log'
2025-01-09 18:41:27.4868 Debug FileTarget(Name=targetFile_wrapped): Creating file appender: 'D:\src-hpg\HPG.Prototypes\HPG.Proto.NLoggerForNetCore\HPG.Proto.NLoggerForNetCore.ConsoleAppHosted\bin\Debug\net8.0\HOT-CP-PF51VH27__2025-01-09T174127Z__pid55280.log'
How should the json configuration for NLog look like to achieve the behaviour I'm looking for?
I'm using NLog.Extensions.Logging
v5.3.15 (NLog
v5.3.4) in my .Net 8 Console Application.
I want, that loggers called Microsoft.*
write with minimum Log-level Info
and all the rest of loggers with minimum log-Level Trace
. Both rules should write to the same target (of type File
).
My rules-section (json) looks like the following:
"rules": [
{
"logger": "Microsoft.*",
"enabled": true,
"level": "Info",
"ruleName": "ruleInfo",
"writeTo": "targetFile",
"final": true
},
{
"logger": "*",
"enabled": true,
"minLevel": "Trace",
"ruleName": "ruleTrace",
"writeTo": "targetFile"
}
]
However, this configuration writes all loggers with minimum log-level Trace
.
The NLog internal Log-file (in Debug-level) shows that this Rule-configuration obviously is not the correct one:
2025-01-09 18:41:27.2665 Debug Message Template Auto Format enabled
2025-01-09 18:41:27.3474 Debug Wrapping target 'targetFile' with AsyncTargetWrapper and renaming to 'targetFile_wrapped'
2025-01-09 18:41:27.3474 Debug Adding target NLog.Targets.Wrappers.AsyncTargetWrapper(Name=targetFile)
2025-01-09 18:41:27.3474 Info Registered target NLog.Targets.Wrappers.AsyncTargetWrapper(Name=targetFile)
2025-01-09 18:41:27.3474 Info NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 5.3.4.2778. Product version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b. GlobalAssemblyCache: False
2025-01-09 18:41:27.3474 Debug --- NLog configuration dump ---
2025-01-09 18:41:27.3474 Debug Targets:
2025-01-09 18:41:27.3474 Debug AsyncWrapper_FileTarget(Name=targetFile)
2025-01-09 18:41:27.3474 Debug Rules:
2025-01-09 18:41:27.3474 Debug logNamePattern: (Microsoft.:StartsWith) levels: [ Info ] writeTo: [ targetFile ] final: True
2025-01-09 18:41:27.3474 Debug logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] writeTo: [ targetFile ]
2025-01-09 18:41:27.3474 Debug --- End of NLog configuration dump ---
2025-01-09 18:41:27.3750 Info Validating config: TargetNames=targetFile, ConfigItems=22 ConfigSection=NLog
2025-01-09 18:41:27.3750 Debug Unused target checking is started... Rule Count: 2, Target Count: 1
2025-01-09 18:41:27.3802 Debug Unused target checking is completed. Total Rule Count: 2, Total Target Count: 1, Unused Target Count: 0
2025-01-09 18:41:27.3955 Info Configuration initialized.
2025-01-09 18:41:27.4161 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Extensions.Hosting.Internal.ApplicationLifetime
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Trace] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Debug] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Info] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Warn] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Error] => targetFile
2025-01-09 18:41:27.4161 Debug Logger Microsoft.Extensions.Hosting.Internal.ApplicationLifetime [Fatal] => targetFile
2025-01-09 18:41:27.4238 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Extensions.Hosting.Internal.Host
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Trace] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Debug] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Info] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Warn] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Error] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Extensions.Hosting.Internal.Host [Fatal] => targetFile
2025-01-09 18:41:27.4238 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Hosting.Lifetime
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Trace] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Debug] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Info] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Warn] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Error] => targetFile
2025-01-09 18:41:27.4238 Debug Logger Microsoft.Hosting.Lifetime [Fatal] => targetFile
2025-01-09 18:41:27.4573 Debug Targets configured when LogLevel >= Trace for Logger: HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Trace] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Debug] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Info] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Warn] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Error] => targetFile
2025-01-09 18:41:27.4573 Debug Logger HPG.Proto.NLoggerForNetCore.ConsoleAppHosted.MyHostedService [Fatal] => targetFile
2025-01-09 18:41:27.4573 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Extensions.Hosting.BackgroundService
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Trace] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Debug] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Info] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Warn] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Error] => targetFile
2025-01-09 18:41:27.4573 Debug Logger Microsoft.Extensions.Hosting.BackgroundService [Fatal] => targetFile
2025-01-09 18:41:27.4868 Debug FileTarget(Name=targetFile_wrapped): Preparing for new file: 'D:\src-hpg\HPG.Prototypes\HPG.Proto.NLoggerForNetCore\HPG.Proto.NLoggerForNetCore.ConsoleAppHosted\bin\Debug\net8.0\HOT-CP-PF51VH27__2025-01-09T174127Z__pid55280.log'
2025-01-09 18:41:27.4868 Debug FileTarget(Name=targetFile_wrapped): Creating file appender: 'D:\src-hpg\HPG.Prototypes\HPG.Proto.NLoggerForNetCore\HPG.Proto.NLoggerForNetCore.ConsoleAppHosted\bin\Debug\net8.0\HOT-CP-PF51VH27__2025-01-09T174127Z__pid55280.log'
How should the json configuration for NLog look like to achieve the behaviour I'm looking for?
Share Improve this question asked 2 days ago tommijotommijo 651 silver badge7 bronze badges 3 |1 Answer
Reset to default 1NLog v5 introduces the FinalMinLevel, which you can use like this:
"rules": [
{
"logger": "Microsoft.*",
"finalMinLevel": "Info"
},
{
"logger": "*",
"enabled": true,
"minLevel": "Trace",
"ruleName": "ruleTrace",
"writeTo": "targetFile"
}
]
FinalMinLevel
allows you to suppress logging-output from matching Loggers. The above configuration will allow LogLevel Info
(And more severe) but suppress Debug
+ Trace
.
本文标签:
版权声明:本文标题:.net - Configuring different Loggers in NLog to output in different log-levels, but to the same target - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736620712a1945569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"level": "Info"
+"final": true
+"enabled": true
+"writeTo": "targetFile"
and replace with"finalMinLevel": "Info"
. See also: github.com/NLog/NLog/wiki/Logging-Rules-FinalMinLevel – Rolf Kristensen Commented yesterday