Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

c# - Buffering log messages in NLog and manually flushes them to target

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...