1

I would like to use Layouts in NLog to be able to change the minimum log level using an external variable :

<nlog>
   <variable name="loglevel" value="Debug"/>
   <targets>
      <target ... /> 
   </targets>
   <rules>
      <logger name="*" minlevel="${loglevel}" writeTo="LogFile" />
   </rules>
</nlog>

After starting NLog, all log levels (eg : Tracing, Debug, Info, ...) are set to false which indicate NLog failed to parse minlevel attribute properly.

The NLog layout feature seems to works with target attributes only. What I want to achieve : in my real app, loglevel is not a constant but rather a custom layout renderer.

I have also tried to replace value="Debug" by value="LogLevel.Debug" without success.

tigrou
  • 4,236
  • 5
  • 33
  • 59

1 Answers1

0

** Updated Answer **

NLog ver. 4.6 added support for using NLog-Config-Variables in minLevel. See https://github.com/NLog/NLog/pull/2709

NLog ver. 4.6.7 added support for adjusting minLevel at runtime, by modifying NLog-Config-Variables and calling ReconfigExistingLoggers(). See https://github.com/NLog/NLog/pull/3184

** Original Answer **

The minlevel, maxlevel and level attributes on <logger> should be fixed strings.

In this case you could use a <filter>, e.g.

<nlog>
   <variable name="loglevel" value="Debug"/>
   <targets>
      <target ... /> 
   </targets>
   <rules>
      <logger name="*" writeTo="LogFile" >
          <filter condition="${level} >= ${loglevel}" action="Log">
      </logger>
   </rules>
</nlog>

See the docs

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Julian
  • 33,915
  • 22
  • 119
  • 174
  • The filter condition works but only if defined as a constant eg : `level >= LogLevel.Debug`. I was not able to make it work as a variable : `level >= ${loglevel}`. Also the syntax to use seems to be slightly different syntax than yours, see here : https://github.com/NLog/NLog/wiki/Conditions – tigrou Aug 24 '16 at 08:09