本文介绍了如何使用环境变量控制Nlog logLevel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用环境变量覆盖Nlog配置中的LogLevel,但它不起作用:例如

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>

其中LOG_LEVEL设置为LogLevel.Info

where LOG_LEVEL is set to LogLevel.Info

与Nlog Env var相同的配置可以工作:

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>

任何提示如何利用env变量?要写一些扩展名吗?

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

推荐答案

我知道这有点罗word,但也许可以奏效,直到有人创建适当的修复程序为止:

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>

顺便说一句.很好奇为什么您原来的问题是小于问题而不是大于问题.在将LOG_LEVEL配置为警告"时会期望的,那么它应该记录所有警告或更严重的警告.

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.

上面的示例将对性能产生影响,因为查找环境变量的速度并不快. NLog 4.6.8引入了cachedSeconds功能,从而降低了性能影响:

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还使在LoggingRules中使用Layout更容易,但是仍然需要一个人对LogManager.ReconfigExistingLoggers()进行显式调用才能激活.

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.

另请参阅: https://github.com/nlog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

这篇关于如何使用环境变量控制Nlog logLevel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 22:15