Update NLog ver. 4.6.7
NLog 4.6.7 makes it easier to use Layout in LoggingRules:
<logger minLevel="${environment:LOG_LEVEL}" />
But one is still required to make an explicit call to LogManager.ReconfigExistingLoggers()
to activate. So maybe have a background-thread that monitors for changes in environment-variable, that triggers the explicit call.
See also: https://github.com/nlog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
See also: https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration
Original (Older NLog)
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>