In NLog, is it possible to use layouts to define the log level?

旧街凉风 提交于 2019-12-11 04:55:05

问题


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.


回答1:


** 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



来源:https://stackoverflow.com/questions/39099395/in-nlog-is-it-possible-to-use-layouts-to-define-the-log-level

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