在 NLog 中缓冲日志消息并手动将其刷新到目标

发布于 2024-12-16 10:39:13 字数 839 浏览 6 评论 0原文

我正在尝试通过 NLog MailTarget 进行日志记录。 它工作得很好,但我想用 BufferedTargetWrapper 包装邮件目标来缓冲日志消息,直到预定义的代码点,我想手动刷新缓冲区并通过单个发送先前缓冲的日志消息邮件(如邮件目标中定义的)。

如果我定义了 FlushTimeoutBufferedTargetWrapperBufferSize ,一切仍然按预期正常工作。 但如果未设置 FlushTimeout 和 BufferSize,我将无法使其工作。

就像在这个问题上回答的那样 强制 BufferingTargetWrapper 为空 i没有类似的东西:

 LogManager.Configuration.AllTargets.Where(t => t != null && t is BufferingTargetWrapper).ToList().
            ForEach(b => ((BufferingTargetWrapper)b).Flush(null));

但是文档和这个答案与我的 NLog 版本(2.0.0.2000)相矛盾。 没有不带参数的flush方法,只有异步目标的flush方法。

有没有办法强制 BufferingTargetWrapper 将所有记录的消息刷新到包装的目标(通过邮件发送)?

I am trying to log via the NLog MailTarget.
It works just fine, but i wanted to wrap the mailtarget with the BufferedTargetWrapper to buffer the log messages until a predefined codepoint, where i want to manually flush the buffer and send the previusly buffered log messages by an single mail (like defined in the mail target).

If I define a FlushTimeout or the BufferSize of the BufferedTargetWrapper everything still works just fine as supposed.
But if the FlushTimeout and the BufferSize is not set, i can't get it to work.

Like answered in this question on SO Force BufferingTargetWrapper to empty i have no something like that:

 LogManager.Configuration.AllTargets.Where(t => t != null && t is BufferingTargetWrapper).ToList().
            ForEach(b => ((BufferingTargetWrapper)b).Flush(null));

But the documentation and this answer is contradictory to my NLog Version (2.0.0.2000).
There is no flush method without parameters, only the flush method for asynchronous targets.

Is there a way to force the BufferingTargetWrapper to flush all the logged messages to the wrapped target (to send it by mail)?

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

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

发布评论

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

评论(1

叫思念不要吵 2024-12-23 10:39:13

根据文档,您的方法不应该起作用,但没关系。只需给flush方法一个空的lambda表达式:

LogManager.Configuration.AllTargets
    .OfType<BufferingTargetWrapper>()
    .ToList()
    .ForEach(b => b.Flush(e =>
        {
            //do nothing here
        }));

According to the documentation your approach should not work, but it is fine. Just give the flush method an empty lambda expression:

LogManager.Configuration.AllTargets
    .OfType<BufferingTargetWrapper>()
    .ToList()
    .ForEach(b => b.Flush(e =>
        {
            //do nothing here
        }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文