简体   繁体   English

nLog,有没有一种方法可以记录错误时跟踪级别提升消息的尾部?

[英]nLog, Is there a way to log a tail of trace level up messages on error?

I am looking for a way to dump a tail of trace level logging (and up) when an error occurs. 我正在寻找一种在发生错误时转储跟踪级别日志记录尾部的方法。

Is there a way to do this? 有没有办法做到这一点?

Sorry, but NLog has no built-in target for this feature. 抱歉,NLog对此功能没有内置目标。 But you can easy implement this! 但是您可以轻松实现! Create custom target based on CompoundTargetBase class! 根据CompoundTargetBase类创建自定义目标!

Here my simple prototype: 这是我的简单原型:

class TracedErrorTarget : CompoundTargetBase
{
    private int _head;
    private LogEventInfo[] _traceTail; // ring buffer

    protected override void Write(LogEventInfo logEvent)
    {
        if (logEvent.Level == LogLevel.Trace)
        {
            SaveTrace(logEvent);
            return;
        }

        if (logEvent.Level == LogLevel.Error)
        {
            LogEventInfo tracedLogEvent = PrepareTracedErrorEvent(logEvent);
            base.Write(tracedLogEvent);
            return;
        }

        base.Write(logEvent);
    }

    private void SaveTrace(LogEventInfo logEvent)
    {
        int traceDepth = _traceTail.Length;

        _traceTail[_head] = logEvent;
        _head = (_head + 1) % traceDepth;
    }

    private LogEventInfo PrepareTracedErrorEvent(LogEventInfo logEvent)
    {
        // prepare LogEventInfo with trace tail
        throw new System.NotImplementedException();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM