无法记录时,NLog FallBackgroup日志异常

时间:2014-08-11 08:28:54

标签: c# exception logging nlog fallback

我的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未能在第一个实例中记录异常的异常/消息?

谢谢,

1 个答案:

答案 0 :(得分:3)

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

一样启用内部日志