NLog FallBackgroup日志异常,日志失败

本文关键字:日志 失败 异常 NLog FallBackgroup | 更新日期: 2023-09-27 18:17:20

我的NLog配置中有以下内容。

<targets>
  <target xsi:type="FallbackGroup" name="MainTarget">
    <target xsi:type="AsyncWrapper" name="MyDataBaseLogger">
      <target xsi:type="MyTarget" />
    </target>
    <target xsi:type="AsyncWrapper" name="MyFileLogger">
      <target xsi:type="MyTarget2" />
    </target>        
  </target>
</targets>

MyTarget.cs如下:

   [Target("MyTarget")]
    public class MyTarget : TargetWithLayout
    {
        protected override void Write(LogEventInfo logEvent)
        {
            WriteLogEvent(logEvent);
        }
        protected override void Write(AsyncLogEventInfo[] logEvents)
        {
            foreach (var logEvent in logEvents)
            {
                WriteLogEvent(logEvent.LogEvent);
            }
        }
        private void WriteLogEvent(LogEventInfo logEvent)
        {
            throw new Exception("My Logger Failed");
            Debug.WriteLine(logEvent.FormattedMessage);
        }
    }

MyTarget2.cs

[Target("MyTarget2")]
public class MyTarget2 : TargetWithLayout
{
    protected override void Write(LogEventInfo logEvent)
    {
        WriteLogEvent(logEvent);
    }
    protected override void Write(AsyncLogEventInfo[] logEvents)
    {
        foreach (var logEvent in logEvents)
        {
            WriteLogEvent(logEvent.LogEvent);
        }
    }
    private void WriteLogEvent(LogEventInfo logEvent)
    {
        Debug.WriteLine(logEvent.FormattedMessage);
    }
}

所以在这种情况下,如果MyTarget失败(在这个例子中它失败了),它会转到MyTarget2,它确实失败了。但是,我如何记录MyTarget在第一个实例中未能记录异常的异常/消息?

谢谢,

NLog FallBackgroup日志异常,日志失败

了解目标失败原因的唯一方法是启用内部日志并查看输出。内部日志可以像这里描述的那样启用,https://github.com/NLog/NLog/wiki/Internal-Logging