【问题标题】:NLog config rule not taking affectNLog 配置规则不生效
【发布时间】:2018-08-08 22:33:12
【问题描述】:

我的项目有以下 nlog.config 文件。当我在本地调试时,它按预期工作,Hangfire 消息被过滤为仅显示 Warn 及以上。然而,在我们的登台服务器(IIS 8.5)上,nlog 似乎忽略了规则,只是将所有内容(包括 Info)记录到 elmah:

  <?xml version="1.0" encoding="utf-8" ?>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extensions>
    <add assembly="NLog.Elmah"/>
  </extensions>
  <targets>
    <target xsi:type="Elmah" name="elmahWithLogLevelAsType" layout="${message}" LogLevelAsType="true"/>
  </targets>
  <rules>
    <logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
    <logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
  </rules>
</nlog>

即使我删除了 Hangfire.* 规则并将总括更改为 minlevel="Warn",它仍然会记录 Info 项。

【问题讨论】:

  • 您确定您的 bin 文件夹中没有第二个 nlog.config 吗?
  • @Julian 不,肯定只有一个 nlog.config

标签: nlog elmah nlog-configuration


【解决方案1】:

认为您正在运行两个不同版本的 NLog。

这将捕获所有带有警告(及以上级别)的日志事件:

<logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />

这意味着所有信息或以下的日志事件都将尝试下一条规则:

<logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />

试试这个配置:

<?xml version="1.0" encoding="utf-8" ?>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extensions>
    <add assembly="NLog.Elmah"/>
  </extensions>
  <targets>
    <target xsi:type="Elmah" name="elmahWithLogLevelAsType" layout="${message}" LogLevelAsType="true"/>
  </targets>
  <rules>
    <logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
    <logger name="Hangfire.*" maxLevel="Warn" final="true" /> <!-- BlackHole -->
    <logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
  </rules>
</nlog>

【讨论】:

  • 遗憾的是这不起作用。此外,即使我将总括 (*) 设置为“警告”,我仍然会收到信息级别的消息。另外,“最终”不是意味着“不再处理任何规则”吗?那为什么会跌到下一个层次呢?
  • @benpage 这些 info-level-messages 的记录器名称(带有命名空间)是什么?也许将布局更新为layout="${logger}|${level}|${message}${exception:format=tostring}"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多