有没有办法通过变量设置NLog记录器的最小级别? [英] Is there a way to set the minlevel of a NLog logger via a variable?

查看:178
本文介绍了有没有办法通过变量设置NLog记录器的最小级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NLog v4.4.12,我将其保存在App.config中

 <nlog>
    <include file="..\Common\Logs\Variables.xml" />
    <rules>
        <logger name="*" minlevel="${logLevel}" writeTo="LogFile, Wcf"/>
    </rules>
</nlog>
 

这是我的Variables.xml文件内容

 <?xml version="1.0" encoding="utf-8"?>
<nlog autoReload="true">
  <variable name="logLevel" value="Fatal" />
</nlog>
 

但是启动我的应用程序时出现异常

Unknown log level: ${logLevel}

我做错什么了吗?或者只是不可能?

目标是最终在每个项目中都有一个xml文件,该文件需要记录事物,以便每个项目都可以拥有自己的最小级别,并能够在运行时通过此xml版本对其进行更改.

编辑:在引发异常之前添加此代码,表明我的变量在那里并具有所需的值.

var nl = NLog.LogManager.Configuration;
if (nl != null)
{
    if (nl.Variables.ContainsKey("logLevel"))
    {
            Console.WriteLine(nl.Variables["logLevel"]);
    }
}

解决方案

**更新后的答案**

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

**原始答案**

不幸的是,您不能在诸如minLevel,level等的<logger>属性中使用布局渲染器(${...})

有两种选择:

使用过滤器

  <logger name="*"  writeTo="LogFile, Wcf">
      <filters>
          <when condition="(level &lt; ${logLevel})" action="Ignore"/>
      </filters>      
 </logger>
 

缺点:

  • 可读性较低
  • 与minLevel属性相比,性能更差

更改代码中的规则

 var rule = config.LoggingRules[0];
// disable old levels, enable new
rule.DisableLoggingForLevel(LogLevel.Debug);
rule.DisableLoggingForLevel(LogLevel.Trace);
rule.EnableLoggingForLevels(LogLevel.Info, LogLevel.Fatal);

//apply config
LogManager.Configuration = config;
 

Using NLog v4.4.12, I have this in my App.config

<nlog>
    <include file="..\Common\Logs\Variables.xml" />
    <rules>
        <logger name="*" minlevel="${logLevel}" writeTo="LogFile, Wcf"/>
    </rules>
</nlog>

And here is my Variables.xml file content

<?xml version="1.0" encoding="utf-8"?>
<nlog autoReload="true">
  <variable name="logLevel" value="Fatal" />
</nlog>

But I get an Exception when launching my app

Unknown log level: ${logLevel}

Am I doing something wrong or is it just impossible?

The goal of this is to eventually have an xml file per project that need to log things so each project can have his own minlevel and be able to change it at runtime via the edition of this xml.

Edit: Adding this code just before the Exception is thrown shows that my variable is there with the desired value.

var nl = NLog.LogManager.Configuration;
if (nl != null)
{
    if (nl.Variables.ContainsKey("logLevel"))
    {
            Console.WriteLine(nl.Variables["logLevel"]);
    }
}

解决方案

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

Unfortunately you can't use layout renderers (the ${...}) in the <logger> attributes like minLevel, level, etc

There are two options:

Use filters

 <logger name="*"  writeTo="LogFile, Wcf">
      <filters>
          <when condition="(level &lt; ${logLevel})" action="Ignore"/>
      </filters>      
 </logger>

Downsides:

  • less readable
  • hurt performance more compared to the minLevel attribute

Change the rules in code

var rule = config.LoggingRules[0];
// disable old levels, enable new
rule.DisableLoggingForLevel(LogLevel.Debug);
rule.DisableLoggingForLevel(LogLevel.Trace);
rule.EnableLoggingForLevels(LogLevel.Info, LogLevel.Fatal);

//apply config
LogManager.Configuration = config;

这篇关于有没有办法通过变量设置NLog记录器的最小级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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