有损日志记录,在错误后还会收到一些消息吗?

发布于 2024-11-14 10:06:19 字数 107 浏览 4 评论 0原文

log4net 中的有损日志记录非常适合以最小的性能损失获取导致错误的事件的详细信息。然而,在错误发生后记录一些消息以验证系统是否按计划处理它通常会很有趣。怎么办呢?

Lossy logging in log4net is great to get the details of events leading to an error with minimal performance-loss. However it would often be interesting to also log a few messages after the error to verify that the system handled it as planned. How can it be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2024-11-21 10:06:19

我会编写 BufferingForwardAppender 的自定义版本,其中发生错误后会记录可配置数量的消息。基本上,您会在错误一段时间后关闭“有损”并直接转发所有消息。

注意:请确保当记录错误时,appender 立即刷新消息,因为您无法确定错误后是否还会有其他消息。

I would write a custom version of the BufferingForwardAppender, which would log a configurable number of message after an error. Basically you turn off "being lossy" after an error for a while and forward all messages directly.

Note: Make sure that the appender flushes the messages immediately when an error is logged because you cannot be sure that there will be other messages after the error.

天涯离梦残月幽梦 2024-11-21 10:06:19

被接受的答案就像一个魅力。为了简化我没有对错误后的消息数量进行单独设置。相反,在触发消息后,会无损发送一个完整缓冲区 + 1。代码:

public class BufferingForwardingAppenderEx : BufferingForwardingAppender
{
    /// <summary>
    /// If in lossy-mode, set the appender to non-lossy after a send has been done to get what happended after.
    /// </summary>
    public bool NonLossyAfterTriggered { get; set; }

    // Flag that tells if the appender is currently in non-lossy-override-mode
    private bool _nonLossyOverride = false;

    protected override void SendBuffer(global::log4net.Core.LoggingEvent[] events)
    {
        if (NonLossyAfterTriggered)
        {
            SetLossyAccordingToReasonForSending(events);
        }

        base.SendBuffer(events);
    }

    private void SetLossyAccordingToReasonForSending(LoggingEvent[] events)
    {
        if (_nonLossyOverride)
        {
            var newestEvent = events[events.Length - 1];
            var bufferIsSentBecauseItIsFull = !Evaluator.IsTriggeringEvent(newestEvent);

            if (bufferIsSentBecauseItIsFull)
            {
                _nonLossyOverride = false;
                Lossy = true;
            }
        }
        else
        {
            _nonLossyOverride = true;
            Lossy = false;
        }
    }
}

下面是一个如何配置的示例:

<appender name="BufferingForwardingAppender"
          type="Foo.log4net.BufferingForwardingAppenderEx">
  <bufferSize value="3"/>
  <lossy value="true"/>
  <nonLossyAfterTriggered value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>
  <appender-ref ref="ConsoleAppender"/>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
      value="%date{yyyy-MM-dd HH:mm:ss} %-5level %-13logger{1} %message%newline"/>
  </layout>
</appender>

<root>
  <level value="DEBUG"/>
  <appender-ref ref="BufferingForwardingAppender"/>
</root>

使用此配置,一个单独的警告(或更糟)前面是 2 条消息,后面是 4 条消息。

The accepted answer worked like a charm. To simplify I did not make a separate setting for the number of messages after the error. Instead one full buffer + 1 is sent non-lossy after a triggering message. The code:

public class BufferingForwardingAppenderEx : BufferingForwardingAppender
{
    /// <summary>
    /// If in lossy-mode, set the appender to non-lossy after a send has been done to get what happended after.
    /// </summary>
    public bool NonLossyAfterTriggered { get; set; }

    // Flag that tells if the appender is currently in non-lossy-override-mode
    private bool _nonLossyOverride = false;

    protected override void SendBuffer(global::log4net.Core.LoggingEvent[] events)
    {
        if (NonLossyAfterTriggered)
        {
            SetLossyAccordingToReasonForSending(events);
        }

        base.SendBuffer(events);
    }

    private void SetLossyAccordingToReasonForSending(LoggingEvent[] events)
    {
        if (_nonLossyOverride)
        {
            var newestEvent = events[events.Length - 1];
            var bufferIsSentBecauseItIsFull = !Evaluator.IsTriggeringEvent(newestEvent);

            if (bufferIsSentBecauseItIsFull)
            {
                _nonLossyOverride = false;
                Lossy = true;
            }
        }
        else
        {
            _nonLossyOverride = true;
            Lossy = false;
        }
    }
}

And here is an example of how it is configured:

<appender name="BufferingForwardingAppender"
          type="Foo.log4net.BufferingForwardingAppenderEx">
  <bufferSize value="3"/>
  <lossy value="true"/>
  <nonLossyAfterTriggered value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>
  <appender-ref ref="ConsoleAppender"/>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
      value="%date{yyyy-MM-dd HH:mm:ss} %-5level %-13logger{1} %message%newline"/>
  </layout>
</appender>

<root>
  <level value="DEBUG"/>
  <appender-ref ref="BufferingForwardingAppender"/>
</root>

With this configuration a lone warning (or worse) is preceeded by 2 and followed by 4 messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文