当条件等于不起作用时的 Nlog 过滤器
Nlog filter when condition with equals not working
我必须从启动时传递变量值 class
LogManager.Configuration.Variables["environment"] = "Development";
我在 nlog.config 文件中添加了以下过滤器
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters>
<when condition="equals('${var:environment}', 'Development')" action="Ignore" />
</filters>
</logger>
</rules>
即使我将值作为 Development 传递,消息仍会被记录而不是被忽略。
但是,当我对值进行硬编码时它起作用了
您找到了一个 bug in NLog,但如果您这样做应该会起作用(也会更快):
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters defaultAction='log'>
<when condition="'${var:environment}' == 'Development'" action="Ignore" />
</filters>
</logger>
</rules>
请注意,您还可以在 minLevel
中使用布局。前任。 minLevel="${var:EnvironmentMinLevel:whenEmpty=Error}"
,比<filters>
快多了。另见 https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
NLog.LogManager.Configuration.Variables["EnvironmentMinLevel"] = "Off";
NLog.LogManager.ReconfigExistingLoggers();
我必须从启动时传递变量值 class
LogManager.Configuration.Variables["environment"] = "Development";
我在 nlog.config 文件中添加了以下过滤器
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters>
<when condition="equals('${var:environment}', 'Development')" action="Ignore" />
</filters>
</logger>
</rules>
即使我将值作为 Development 传递,消息仍会被记录而不是被忽略。
但是,当我对值进行硬编码时它起作用了
您找到了一个 bug in NLog,但如果您这样做应该会起作用(也会更快):
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters defaultAction='log'>
<when condition="'${var:environment}' == 'Development'" action="Ignore" />
</filters>
</logger>
</rules>
请注意,您还可以在 minLevel
中使用布局。前任。 minLevel="${var:EnvironmentMinLevel:whenEmpty=Error}"
,比<filters>
快多了。另见 https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
NLog.LogManager.Configuration.Variables["EnvironmentMinLevel"] = "Off";
NLog.LogManager.ReconfigExistingLoggers();