如何使用 BufferingForwardingAppender 和 EventLog Appender 来获取电子邮件警报?

发布于 2024-12-12 05:14:36 字数 417 浏览 0 评论 0原文

我需要一些想法来在网络应用程序中实现以下要求。

我在自定义 dll 中使用 log4net 来记录错误。我完成了 log4net 实现并且工作正常。[aspx 错误记录在 EventLog 中,asp 错误记录在 FileAppender 中]。所有 loggerError() 方法都在自定义 dll 中。

现在我想监视日志记录,假设如果出现像 loggerError() 方法在短短 5 分钟内被调用 20 次以上的致命错误或数据库关闭的情况,那么我想跟踪它并向管理员发送电子邮件。

我的想法,

1.设置一个计时器和计数变量来跟踪点击次数。

2.每次点击后检查点击次数和秒数。

3.如果超过阈值限制,则触发电子邮件...

不确定这将如何工作或是否有任何其他方法可以实现它。

提前致谢

I need some idea to implement the following requirement in the web application .

I am using log4net in a custom dll to log the errors. I completed the log4net implementation and its working fine.[aspx errors are logged in EventLog and the asp errors are logged in FileAppender] .All the loggerError() methods are in the custom dll.

Now i want to monitor the logging,suppose if there is a situation like the loggerError() method is called more than 20 times in just 5 mins bse of Fatal error or database is down,then i want to track that and send email to admin.

My ideas,

1.Set a timer and count variable to track the number of hits .

2.After each hit check the number of hits and the secs.

3.If it exceeds the threshold limit .then trigger the email...

Not sure how this will work or is there any other way to achieve it.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-12-19 05:14:36

我建议编写您自己的 Appender 来容纳这个逻辑。 Appender 可以用作另一个 Appender 的包装器(也许是 SmtpAppender?)。 ForwardingAppender 看起来像是“包装器”Appender 的一个很好的例子。当每条日志消息出现时,应用您的时间和严重性逻辑。如果满足您的条件,请将消息转发到“包装的”Appender。

或者,此 Appender 可以包含其自己的用于生成和发送电子邮件的逻辑(或您想要使用的任何通知方案)。

该 Appender 可以通过标准 log4net 配置机制(app.config、单独的配置等)进行配置,因此时间跨度、错误级别、错误计数都可以配置。

以下是如何将类似的东西实现为 Appender 的想法:

public class MultiThresholdNotifyingAppender : AppenderSkeleton
{
  private Queue<LoggingEvent> loggingEventQueue = new Queue<LoggingEvent>();

  private DateTime windowStart;

  public Level LevelThreshold { get; set; }
  public TimeSpan WindowLength { get; set; }
  public int HitThreshold { get; set; }

  public void Append(LoggingEvent loggingEvent)
  {
    if (loggingEvent.Level < LevelThreshold) 
    {
      if (loggingEventQueue.Count == 0) return;

      if (loggingEvent.TimeStamp - windowStart) >= WindowLength)
      {
        //Error level is less than threshold and the time window has elapsed.  Remove any queued LoggingEvents
        //until all LoggingEvents in the queue fall within the time window.
        while (loggingEventQueue.Count > 0 && loggingEventQueue.Peek().TimeStamp - windowStart >= WindowLength)
        {
          loggingEventQueue.Dequeue();
        }

        windowStart = loggingEventQueue.Peek().TimeStamp;

        return;
      }
    }

    //If we got here, then the Error level is >= the threshold.  We want to save the LoggingEvent and we MIGHT
    //want to notify the administrator if the other criteria are met (number of errors within time window).
    loggingEventQueue.Enqueue(loggingEvent);

    //If this is the first error in the queue, start the time window.
    if (loggingEventQueue.Count == 1) windowStart = loggingEvent.TimeStamp;

    //Too few messages to qualify for notification.
    if (loggingEventQueue.Count < HitThreshold - 1) return; 

    //Now we're talking!  A lot of error messages in a short period of time.    
    if (loggingEvent.TimeStamp - windowStart >= WindowLength)
    {
      //Build a notification message for the administrator by concatenating the "rendered" version of each LoggingEvent.
      string message = string.Join(Enviroment.NewLine, loggingEventQueue.Select(le => le.RenderedMessage));
      SendTheMessage(message);
    }

    //After sending the message, clear the LoggingEvents and reset the time window.
    loggingEventQueue.Clear();      
    windowStart = loggingEvent.TimeStamp;
  }

  public void Append(LoggingEvent[] loggingEvents)
  {
    foreach (var le in loggingEvents)
    {
      Append(le);
    }
  }
}

这个想法是在 Appender 上配置您的阈值(可能与您的通知方法一起)。配置您的记录器以将消息发送到此附加程序,以及您想要将其发送到的任何其他附加程序(事件日志、文件等)。当每条日志消息通过时,此附加程序可以在配置的阈值上下文中检查它,并根据需要发送通知。

此代码中很可能存在线程问题(除非 log4net 为您处理该问题),因此您在访问队列时可能需要锁。

请注意,此代码尚未编译也未经过测试。

请参阅此链接,了解 log4net 存储库中的一些示例自定义 Appender:

http://svn.apache.org/viewvc/logging/log4net/trunk/examples/net/2.0/Appenders/

I would suggest writing your own Appender to house this logic. The Appender could be used as a wrapper for another Appender (maybe the SmtpAppender?). The ForwardingAppender looks like a good example of a "wrapper" Appender. As each log message comes in, apply your timing and severity logic. If your criteria are met, forward the message to the "wrapped" Appender.

Alternatively this Appender could contain its own logic for generating and sending email (or whatever notification scheme you would like to use).

This Appender could be configured via the standard log4net configuration mechanism (app.config, separate config, etc) so the time span, error level, error count, could be configurable.

Here is an idea for how something like this might be implemented as an Appender:

public class MultiThresholdNotifyingAppender : AppenderSkeleton
{
  private Queue<LoggingEvent> loggingEventQueue = new Queue<LoggingEvent>();

  private DateTime windowStart;

  public Level LevelThreshold { get; set; }
  public TimeSpan WindowLength { get; set; }
  public int HitThreshold { get; set; }

  public void Append(LoggingEvent loggingEvent)
  {
    if (loggingEvent.Level < LevelThreshold) 
    {
      if (loggingEventQueue.Count == 0) return;

      if (loggingEvent.TimeStamp - windowStart) >= WindowLength)
      {
        //Error level is less than threshold and the time window has elapsed.  Remove any queued LoggingEvents
        //until all LoggingEvents in the queue fall within the time window.
        while (loggingEventQueue.Count > 0 && loggingEventQueue.Peek().TimeStamp - windowStart >= WindowLength)
        {
          loggingEventQueue.Dequeue();
        }

        windowStart = loggingEventQueue.Peek().TimeStamp;

        return;
      }
    }

    //If we got here, then the Error level is >= the threshold.  We want to save the LoggingEvent and we MIGHT
    //want to notify the administrator if the other criteria are met (number of errors within time window).
    loggingEventQueue.Enqueue(loggingEvent);

    //If this is the first error in the queue, start the time window.
    if (loggingEventQueue.Count == 1) windowStart = loggingEvent.TimeStamp;

    //Too few messages to qualify for notification.
    if (loggingEventQueue.Count < HitThreshold - 1) return; 

    //Now we're talking!  A lot of error messages in a short period of time.    
    if (loggingEvent.TimeStamp - windowStart >= WindowLength)
    {
      //Build a notification message for the administrator by concatenating the "rendered" version of each LoggingEvent.
      string message = string.Join(Enviroment.NewLine, loggingEventQueue.Select(le => le.RenderedMessage));
      SendTheMessage(message);
    }

    //After sending the message, clear the LoggingEvents and reset the time window.
    loggingEventQueue.Clear();      
    windowStart = loggingEvent.TimeStamp;
  }

  public void Append(LoggingEvent[] loggingEvents)
  {
    foreach (var le in loggingEvents)
    {
      Append(le);
    }
  }
}

The idea is to configure your threshold (maybe along with your notification method) values on the appender. Configure your loggers to send their messages to this appender, in addition to any other appenders you want to send them to (EventLog, File, etc). As each logging message comes through, this appender can examine it in the context of the configured threshold values and send the notification as necessary.

There could very well be threading issues in this code (unless log4net handles that for you), so you might need a lock when accessing the queue.

Note that this code has not been compiled nor has it been tested.

See this link for some sample custom Appenders in the log4net repository:

http://svn.apache.org/viewvc/logging/log4net/trunk/examples/net/2.0/Appenders/

闻呓 2024-12-19 05:14:36

由于这是针对 Web 应用程序的,因此我建议使用应用程序变量来跟踪最近 10 个错误。下次发生错误时,用新错误替换最旧的错误(如果有必要将错误计数保持在 10 以下)。添加一些逻辑来检查错误的日期,并相应地调整严重级别。

Since this is for a web application, I would suggest using an Application Variable to keep track of the last 10 errors. The next time an error occurs, replace the oldest error (if necessary to keep the error count under 10) with the new error. Put in some logic to check the dates of the error, and adjust the severity level accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文