簡體   English   中英

無法記錄時,NLog FallBackgroup日志異常

[英]NLog FallBackgroup log exception on fail to log

我的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未能在第一個實例中記錄異常的異常/消息?

謝謝,

了解目標失敗原因的唯一方法是啟用內部日志並查看輸出。 可以像這里描述的那樣啟用內部日志, https://github.com/NLog/NLog/wiki/Internal-Logging

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM