I have my app writing to two text files, with a few things writing to one specific logger and everything else writing to the main logger.
<target xsi:type="File" name="mainFile"
layout="${longdate} ${uppercase:${level}} ${callsite} ${message} ${exception:format=ToString:maxInnerExceptionLevel=5:innerFormat=ToString}"
fileName="${basedir}/AppLog.log"
/>
<target xsi:type="File" name="specificFile"
layout="${longdate} ${uppercase:${level}} ${callsite} ${message} ${exception:format=ToString:maxInnerExceptionLevel=5:innerFormat=ToString}"
fileName="${basedir}/other/SpecificAppLog.log"
/>
</targets>
<rules>
<logger name="SpecificLogger" minlevel="Debug" writeTo="specificFile" final="true" />
<logger name="*" minlevel="Debug" writeTo="mainFile" />
</rules>
When both loggers have the same minlevel, everything works fine: Every log line goes to one of the two files, never both, and always to the expected file.
But if I change SpecificLogger from Debug to Info, leaving the main logger at Debug, all of the Debug lines start writing to the main file!
The intended/expected result I need is that if I change SpecificLogger's minlevel from Debug to Info, then all logs at Debug level should not be written at all.
Instead, it looks like the SpecificLogger Debug lines are "spilling over" to the main logger, if I set that logger's minlevel higher.
How do I stop those Debug lines from writing?
Adding the final="true" did not solve it.
The final="true"
is special, and means that any that the logging-rule matches will not reach the following rules. When you specify minLevel="Info"
, then you are saying that any else (LogLevel.Trace + LogLevel.Debug) should flow down to the following rules.
With NLog v4 then you can do like this:
<rules>
<logger name="SpecificLogger" minlevel="Info" writeTo="specificFile" />
<logger name="SpecificLogger" maxlevel="Fatal" final="true" /> <!-- Blackhole -->
<logger name="*" minlevel="Debug" writeTo="mainFile" />
</rules>
With NLog v5 then you can use finalMinLevel
that tries to be more user-friendly:
<rules>
<logger name="SpecificLogger" minlevel="Info" writeTo="specificFile" finalMinLevel="Fatal" />
<logger name="*" minlevel="Debug" writeTo="mainFile" />
</rules>
See also: https://github.com/NLog/NLog/wiki/Logging-Rules-FinalMinLevel