时间流逝或整数达到指定限制 - 怎么办? (C#)

发布于 2024-09-25 20:58:32 字数 188 浏览 1 评论 0原文

我正在运行一个 C# 程序(控制台应用程序,很快将转换为 Windows 服务),我需要能够向管理员发送有关服务中错误的电子邮件,但我需要它不要针对每个错误向我们发送电子邮件,如果最近几分钟的错误数量超过 4-5 个,因此它只会发送一封电子邮件,说明存在多个错误。

我知道我会以某种形式使用计时器,但是任何人都可以提供更具体的建议吗?我将非常感激

I'm running a C# program (console application, soon to be converted to a Windows Service) where I need to be able to email administrators about errors in the service, but I need it to not send our an email for every error if the number of errors in the last few minutes exceeds 4-5, so it'll only send one email saying there are multiple errors.

I understand I'd use a timer in someform, but can anyone offer any more specific advice? I'd be very grateful

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

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

发布评论

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

评论(3

孤凫 2024-10-02 20:58:32

修改自MSDN。请注意有关 Timer 对象 aTimer 声明和清理的注释。

using System;
using System.Timers;
using System.Threading;

public class Timer2
{
    private static System.Timers.Timer aTimer;
    private static List<string> errors = new List<string>();
    private static readonly int interval = 300000;  // 5 minutes at present
    private static readonly int trigger = 10;       // send msg if > 10 errors

    // Message processing - error detection
    public static void processMessage(Message message)
    {
      // do the work here
      // then check error
      if (message.HasError)
      {
        // add error to pending list
        lock (errors)
        {
          string newErrorData = "got another one!";
          errors.Add(newErrorData);
          ++trigger;
        }
      }
    }

    public static void Main()
    {
        // Normally, the timer is declared at the class level,
        // so that it stays in scope as long as it is needed.
        // If the timer is declared in a long-running method,  
        // KeepAlive must be used to prevent the JIT compiler 
        // from allowing aggressive garbage collection to occur 
        // before the method ends. (See end of method.)
        //System.Timers.Timer aTimer;

        // Create a timer with specified interval.
        aTimer = new System.Timers.Timer(interval);

        // Hook up the event handler for the Elapsed event.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Enabled = true;

        // Kick off message handling - don't forget to clean up the timer when 
        // you wish to exit
        while (moreMessages)
        {
           Message message = getNextmessage();
           ProcessMessage(message);
        }
        // cleanup here when messages are drained
        // If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);        }

    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    {
        object errorEmail = null;
        lock (errors)
        {
            if (errors.Count > trigger)
            {
               // init message to contain errors here
               errorEmail = new ErrorEmail();
               foreach (string err in errors)
               {
                  // add error info to message
               } 
               errors.Clear();
               trigger = 0;
            }
        }
        if (errorEmail != null)
        {
          // send message outside the lock
          Send(errorEmail);
        }
    }
}

Modified from MSDN. Note comments about declaration and cleanup of Timer object aTimer.

using System;
using System.Timers;
using System.Threading;

public class Timer2
{
    private static System.Timers.Timer aTimer;
    private static List<string> errors = new List<string>();
    private static readonly int interval = 300000;  // 5 minutes at present
    private static readonly int trigger = 10;       // send msg if > 10 errors

    // Message processing - error detection
    public static void processMessage(Message message)
    {
      // do the work here
      // then check error
      if (message.HasError)
      {
        // add error to pending list
        lock (errors)
        {
          string newErrorData = "got another one!";
          errors.Add(newErrorData);
          ++trigger;
        }
      }
    }

    public static void Main()
    {
        // Normally, the timer is declared at the class level,
        // so that it stays in scope as long as it is needed.
        // If the timer is declared in a long-running method,  
        // KeepAlive must be used to prevent the JIT compiler 
        // from allowing aggressive garbage collection to occur 
        // before the method ends. (See end of method.)
        //System.Timers.Timer aTimer;

        // Create a timer with specified interval.
        aTimer = new System.Timers.Timer(interval);

        // Hook up the event handler for the Elapsed event.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Enabled = true;

        // Kick off message handling - don't forget to clean up the timer when 
        // you wish to exit
        while (moreMessages)
        {
           Message message = getNextmessage();
           ProcessMessage(message);
        }
        // cleanup here when messages are drained
        // If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);        }

    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    {
        object errorEmail = null;
        lock (errors)
        {
            if (errors.Count > trigger)
            {
               // init message to contain errors here
               errorEmail = new ErrorEmail();
               foreach (string err in errors)
               {
                  // add error info to message
               } 
               errors.Clear();
               trigger = 0;
            }
        }
        if (errorEmail != null)
        {
          // send message outside the lock
          Send(errorEmail);
        }
    }
}
生生不灭 2024-10-02 20:58:32

如果您使用数据库跟踪发送的每封电子邮件,则可以随时轮询数据库,以查看在给定时间段内针对给定错误您看到了多少封电子邮件等。在我参与过的几个项目中,电子邮件是一个很重要的问题。要求,记录发送的电子邮件始终是一个姐妹要求,从而为您的问题创建解决方案。

If you track each email you send using a database, you can always poll the database to see how many emails you have seen for a given error over a given period of time etc. In the few projects I've worked on where emailing was a requirement, logging of sent emails has always been a sister requirement, thus creating a solution to your problem.

や莫失莫忘 2024-10-02 20:58:32

使用将错误保存在列表中,然后使用 System.Threading.Timer< /a>.

传递一个封装 SendEmail 方法的委托。

Use save the errors in a list then use System.Threading.Timer.

Pass a delegate that wraps up a SendEmail method.

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