Buffering log messages in NLog and manually flushes them to target

 ̄綄美尐妖づ 提交于 2019-12-03 19:56:00

问题


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)?


回答1:


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
        }));


来源:https://stackoverflow.com/questions/8153286/buffering-log-messages-in-nlog-and-manually-flushes-them-to-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!