How to control Nlog logLevel using environment variables

好久不见. 提交于 2019-12-02 15:21:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/47475698/how-to-control-nlog-loglevel-using-environment-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!