Trying to override LogLevel in Nlog Configuration using environment variable and it does not work: e.g
I know this is a little more wordy, but maybe it works, until someone creates a proper fix:
<logger name="*" writeTo="console">
<filters>
<when condition="(level >= LogLevel.Fatal and equals('${environment:LOG_LEVEL}','LogLevel.Fatal)" action="Log"/>
<when condition="(level >= LogLevel.Error and equals('${environment:LOG_LEVEL}','LogLevel.Error')" action="Log"/>
<when condition="(level >= LogLevel.Info and equals('${environment:LOG_LEVEL}','LogLevel.Info')" action="Log"/>
<when condition="(level >= LogLevel.Debug and equals('${environment:LOG_LEVEL}','LogLevel.Debug')" action="Log"/>
<when condition="(level >= LogLevel.Trace and equals('${environment:LOG_LEVEL}','LogLevel.Trace')" action="Log"/>
</filters>
</logger>
Btw. curious why your original question has it as less-than instead of greater-than. Would expect when having configured LOG_LEVEL to Warn, then it should log all warnings or worse.
The above example will have a performance hit because lookup of environment-variables are not fast. NLog 4.6.8 introduces the cachedSeconds-features, that reduces the performance hit:
<logger name="*" writeTo="console">
<filters>
<when condition="(level >= LogLevel.Fatal and equals('${environment:LOG_LEVEL:cachedSeconds=5}','LogLevel.Fatal)" action="Log"/>
<when condition="(level >= LogLevel.Error and equals('${environment:LOG_LEVEL:cachedSeconds=5}','LogLevel.Error')" action="Log"/>
<when condition="(level >= LogLevel.Info and equals('${environment:LOG_LEVEL:cachedSeconds=5}','LogLevel.Info')" action="Log"/>
<when condition="(level >= LogLevel.Debug and equals('${environment:LOG_LEVEL:cachedSeconds=5}','LogLevel.Debug')" action="Log"/>
<when condition="(level >= LogLevel.Trace and equals('${environment:LOG_LEVEL:cachedSeconds=5}','LogLevel.Trace')" action="Log"/>
</filters>
</logger>
NLog 4.6.8 also makes it easier to use Layout in LoggingRules, but one is still required to make an explicit call to LogManager.ReconfigExistingLoggers()
to activate.
See also: https://github.com/nlog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules