我的项目有以下 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.*
规则并将 catchall 更改为 minlevel="Warn"
它仍然会记录 Info 项。
最佳答案
假设您正在运行两个不同版本的 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>
关于NLog 配置规则不生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039840/