Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

logging - Combine LimitingWrapper and BufferingWrapper for NLog Mail target

I was hoping to achieve both grouping and throttling of log messages using a combination of the LimitingWrapper and BufferingWrapper targets. Regardless of which target was the inner or outer, I couldn't get it to work.

For example, I would like to group 5 seconds worth of log messages into the same email message (using the BufferingWrapper) but limit it to only send a maximum or 10 emails in an hour using the LimitingWrapper.

Is it possible to achieve this using one or more of the NLog targets?

question from:https://stackoverflow.com/questions/66060033/combine-limitingwrapper-and-bufferingwrapper-for-nlog-mail-target

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

1 Answer

0 votes
by (71.8m points)

Think you could make 2 target-pipelines. First pipeline that fires instantly on first error, and a second pipeline that fires every 6 minutes.

<targets>
   <target type="Mail" name="email" />
   <target type="LimitingWrapper" name="emailInstant" messageLimit="1">
        <target-ref name="email" />
   </target>
   <target type="BufferingWrapper" name="emailLazy" flushTimeout="360">
        <target-ref name="email" />
   </target>
</target>
<rules>
   <logger name="*" minLevel="Warn" writeTo="emailInstant, emailLazy" />
</rules>

See also: https://github.com/NLog/NLog/wiki/LimitingWrapper-target

See also: https://github.com/nlog/NLog/wiki/BufferingWrapper-target

I guess one could extend the NLog LimitingWrapper with a new option messageBatchLimit (PullRequests are welcome). That instead of limiting the individual number of messages, then one could do like this to limit the number of message-batches:

<targets>
   <target type="BufferingWrapper" name="emailbuffer" flushTimeout="5">
        <target type="LimitingWrapper" name="emailBuffer" messageBatchLimit="10">
                <target type="Mail" name="email" />
        </target>
   </target>
<targets>
<rules>
   <logger name="*" minLevel="Warn" writeTo="emailBuffer" />
</rules>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...