在 NLog 中缓冲日志消息并手动将其刷新到目标
我正在尝试通过 NLog MailTarget 进行日志记录。 它工作得很好,但我想用 BufferedTargetWrapper 包装邮件目标来缓冲日志消息,直到预定义的代码点,我想手动刷新缓冲区并通过单个发送先前缓冲的日志消息邮件(如邮件目标中定义的)。
如果我定义了 FlushTimeout
或 BufferedTargetWrapper
的 BufferSize
,一切仍然按预期正常工作。 但如果未设置 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,您的方法不应该起作用,但没关系。只需给flush方法一个空的lambda表达式:
According to the documentation your approach should not work, but it is fine. Just give the flush method an empty lambda expression: