emaillogginglog4netnlog

NLog limit the number of times same error is logged within set period


We are using NLog to send email messages whenever a 'serious' error occurs. It might be happening very often in some cases generating too many messages.

Is there a way to make NLog limit the number of messages it sends on one particular or any error within a set period?

Is there an analogous mechanism in log4net or any other popular logging library?


Solution

  • You can see my answer to a similar log4net question here:

    log4net - any filter for logging only the Nth message?

    In that answer I proposed a custom log4net Filter implementation that allows repeated messages within a configurable time period to be throttled.

    For NLog, the easiest way might be to write a custom Wrapper target. The custom Wrapper target implemenation would examine the incoming logging messages, forwarding if the current message is different than the most recent message (or maybe if a certain amount of time has passed). You could then wrap any existing Target with this custom Wrapper.

    Note that I have not actually written a Wrapper target, or I would try to provide more info.