Semi Dynamic Routing Rules
NLog 4.6.7 makes it easy to change the LogLevel on-the-fly:
<nlog>
<variable name="myLevel" value="Trace" />
<rules>
<logger name="*" minlevel="${var:myLevel}" writeTo="logfile" />
</rules>
</nlog>
And then change it from runtime like this:
LogManager.Configuration.Variables["myLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();
https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
Rewrite and reload NLog.config
But it sounds like you should just perform a search and replace in the NLog.config
-file, and then perform an explicit reload:
<nlog>
<variable name="myLevel" value="Trace" />
<variable name="mySize" value="42" />
<targets>
<target name="logfile" xsi:type="File"
fileName=".\logs\${shortdate}.log"
archiveAboveSize="${mySize}" />
</targets>
<rules>
<logger name="*" minlevel="${var:myLevel}" writeTo="logfile" />
</rules>
</nlog>
After the file-rewrite/update (that changes the two variables) then explicit reload is done like this:
NLog.LogManager.Configuration = NLog.LogManager.Configuration?.Reload();
Alternative to explicit reload would be to use <nlog autoReload="true">
(Then NLog will detect changes to NLog.config
-file and reload automatically).
Rewrite and reload NLog.user.config
If you feel excited and like advanced stuff, then you could also make use of include-files, and have a default NLog.config
that includes NLog.user.config
:
<nlog autoreload="true">
<variable name="myLevel" value="Trace" />
<variable name="mySize" value="42" />
<include file="NLog.user.config" ignoreErrors="true" /> <!-- Can override variables -->
<targets>
<target name="logfile" xsi:type="File"
fileName=".\logs\${shortdate}.log"
archiveAboveSize="${mySize}" />
</targets>
<rules>
<logger name="*" minlevel="${var:myLevel}" writeTo="logfile" />
</rules>
</nlog>
See also: https://github.com/NLog/NLog/wiki/XML-config-include-Example