在NLog中缓冲日志消息并手动将其刷新到目标 [英] Buffering log messages in NLog and manually flushes them to target
问题描述
我正在尝试通过NLog MailTarget登录.
它工作正常,但我想用BufferedTargetWrapper
包装mailtarget来缓冲日志消息,直到预定义的代码点为止,在这里我想手动刷新缓冲区并通过一条邮件发送以前缓冲的日志消息(例如在邮件目标).
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).
如果我定义了FlushTimeout
的FlushTimeout
或BufferSize
,则一切仍然可以正常工作.
但是,如果未设置FlushTimeout和BufferSize,我将无法正常工作.
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.
像在SO >将BufferingTargetWrapper强制为空我没有类似的东西:
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));
但是文档和此答案与我的NLog版本(2.0.0.2000)相矛盾. 没有参数就没有冲洗方法,只有异步目标的冲洗方法.
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.
是否有一种方法可以强制BufferingTargetWrapper将所有记录的消息刷新到包装的目标(通过邮件发送)?
Is there a way to force the BufferingTargetWrapper to flush all the logged messages to the wrapped target (to send it by mail)?
推荐答案
根据文档,您的方法不起作用,但这很好.只需给flush方法一个空的lambda表达式即可:
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
}));
这篇关于在NLog中缓冲日志消息并手动将其刷新到目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!