3

Trying to override LogLevel in Nlog Configuration using environment variable and it does not work: e.g

<logger name="*"  writeTo="console">
          <filters>
              <when condition="level >= '${environment:LOG_LEVEL}' " action="Ignore"/>
          </filters>      
</logger>

where LOG_LEVEL is set to LogLevel.Info

Same config with Nlog Env var works:

<variable name="myvar1" value="LogLevel.Info"/>
 <logger name="*"  writeTo="console">
          <filters>
              <when condition="level >= '${myvar1}' " action="Ignore"/>
          </filters>      
 </logger>

Any hints how to make use of env variables? Some extension to write?

Julian
  • 33,915
  • 22
  • 119
  • 174
lenisha
  • 39
  • 1
  • 7
  • Have you tried to enable the InternalLogger and look for any errors/exceptions ? Remember that changing environment-variables at runtime will many times not take effect until restarting the application. – Rolf Kristensen Nov 24 '17 at 20:48
  • 1
    Yep, looked at the trace it has problem parsing tokens. Looks like NLog does not support that . ```2017-11-24 16:12:09.4999 Debug Setting 'ConditionBasedFilter.condition' to 'level>=${environment:LOG_LEVEL}' 2017-11-24 16:12:09.4999 Warn Error when setting property 'Condition' on 'NLog.Filters.ConditionBasedFilter' Exception: NLog.Conditions.ConditionParseException: Invalid punctuation: $ at NLog.Conditions.ConditionTokenizer.GetNextToken() ``` – lenisha Nov 24 '17 at 21:12
  • 1
    Maybe a work around is to assign an NLog variable to the value of ${environment:LOG_LEVEL} and then use the NLog variable in the when-filter?. – Rolf Kristensen Nov 24 '17 at 21:44
  • 1
    Already reported as bug https://github.com/NLog/NLog/issues/1502 (It has a suggested workaround but it doesn't support less- / greater-than) – Rolf Kristensen Nov 25 '17 at 14:22
  • Is `${var:myvar1}` working? If so, then `${environment:LOG_LEVEL}` should work also – Julian Nov 27 '17 at 13:48
  • 1
    Possible duplicates: https://stackoverflow.com/questions/39099395 + https://stackoverflow.com/questions/47946734 – Rolf Kristensen Mar 03 '19 at 09:40

1 Answers1

10

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>
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70