有损日志记录,在错误后还会收到一些消息吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会编写 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.
被接受的答案就像一个魅力。为了简化我没有对错误后的消息数量进行单独设置。相反,在触发消息后,会无损发送一个完整缓冲区 + 1。代码:
下面是一个如何配置的示例:
使用此配置,一个单独的警告(或更糟)前面是 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:
And here is an example of how it is configured:
With this configuration a lone warning (or worse) is preceeded by 2 and followed by 4 messages.