NLog - 回退组未按预期工作

  • 本文关键字:工作 回退 NLog nlog
  • 更新时间 :
  • 英文 :


我正在使用Nlog的FallbackGroup包装器来设置故障转移日志记录。如果我的第一个目标 - "网络"记录器失败/出错,它应该记录到第二个目标/回退目标"EventLog"。但是当我运行我的控制台应用程序时,我可以看到它只是尝试登录到第一个并且总是失败。它甚至没有尝试记录到"事件日志"。仅供参考,事件日志目标在隔离中工作正常(没有回退设置)。

我已经得到了上面提到的细节,通过在NLog上启用"内部日志记录",它基本上是将所有NLog执行写入文件。

下面是我的 NLog 配置:

<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<targets>
<!-- For logging over network with json layout -->
<target xsi:type="FallbackGroup" name="XXXFallBackLoggingGroup" returnToFirstOnSuccess="true">
<target name="network" xsi:type="Network" address="https://XXXX/api/log">
<layout xsi:type="JsonLayout">
<attribute name="time" layout="${longdate:universalTime=true}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
</layout>
</target>
<target xsi:type="EventLog" name="eventlog" source="xxxx" layout="${level:upperCase=true}${newline}${longdate:universalTime=true}${newline}${message}${newline}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="XXXFallBackLoggingGroup" />
</rules>
</nlog>

这让我发疯。我已经尝试了很多次来修复它,但没有任何效果。任何想法/建议或解决方案,请告诉我。

编辑: 我试图更新目标的名称,但这也不起作用,但是正如我之前提到的,两者都是孤立工作的。另一点,为了测试回退日志记录,我删除了我的 API - 第一个目标,因此它总是抛出 404 错误,但 NLog 仍然试图始终选择第一个目标

我遇到了完全相同的问题。我注意到当throwExceptions属性被激活时。它抛出异常(它无法登录 sql Server 数据库或其他目标)。接下来,它终止进程。

删除此属性,故障转移配置应该可以正常工作。

https://github.com/NLog/NLog/blob/v4.4.12/src/NLog/Internal/ExceptionHelper.cs#L100

这是负责此行为的行。

您可以将throwExcepions=true 更改为throwConfigExceptions=true,如果您仍想了解配置问题。

最新更新