在NLog中,是否可以使用布局来定义日志级别? [英] In NLog, is it possible to use layouts to define the log level?

查看:607
本文介绍了在NLog中,是否可以使用布局来定义日志级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NLog中使用 Layouts 来更改最小日志级别使用外部变量:

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

启动NLog后,所有日志级别(例如:跟踪,调试,信息等)都设置为false,这表明NLog无法正确解析minlevel属性.

NLog布局功能似乎仅适用于target属性. 我要实现的目标:在我的真实应用中,日志级别不是一个常量,而是一个自定义布局渲染器.

我还试图用value="LogLevel.Debug"替换value="Debug",但没有成功.

解决方案

**更新后的答案**

NLog版本. 4.6新增了对在minLevel中使用NLog-Config-Variables的支持.参见 https://github.com/NLog/NLog/pull/2709

NLog版本. 4.6.7通过修改NLog-Config-Variables并调用ReconfigExistingLoggers(),增加了在运行时调整minLevel的支持.参见 https://github.com/NLog/NLog/pull/3184

**原始答案**

<logger>上的minlevelmaxlevellevel属性应为固定字符串.

在这种情况下,您可以使用<filter>,例如

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

请参见文档

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.

解决方案

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

这篇关于在NLog中,是否可以使用布局来定义日志级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆