2017-07-12 33 views
0

我是C#和面向对象编程的新手。我正在创建一个windows服务,每隔10分钟就会ping一次IP地址10次。C# - Windows服务 - 连续Ping请求超时

如果10个请求中有7个超时(Isolate Network Blips),它会发送一封电子邮件通知系统关闭。我有这部分权利。

我面临的问题是通知说系统已启动。

以下是我的代码:

protected override void OnStart(string[] args) 
     { 
       eventLog.WriteEntry("Source: Service Started",EventLogEntryType.SuccessAudit); 
       timer.Enabled = true; 
       timer.Interval = (10 * 60 * 1000); 
       timer.Elapsed += new System.Timers.ElapsedEventHandler(methodStart); 


     } 

public void methodStart(object sender, ElapsedEventArgs e) 
     { 

      Ping p = new Ping(); 
      PingReply r; 
      string s = "SYSTEM-IP-ADDRESS"; 
      int upCounter=0; 
      int downCounter = 0; 

      bool sysDown = false; 
      try 
      { 
       for (int i = 0; i < 10; i++) 
       { 
        r = p.Send(s); 
        if (r.Status == IPStatus.Success) 
        { 
         eventLog.WriteEntry("Ping to " + s.ToString() + "[" + r.Address.ToString() + "]" + " Successful" 
         + " Response delay = " + r.RoundtripTime.ToString() + " ms" + "\n", EventLogEntryType.SuccessAudit); 
         upCounter++; 

        } 
        else 
        { 

         downCounter++; 
        } 
       } 

       if(downCounter>=7) 
       { 
        //LOG ENTRY 
        eventLog.WriteEntry("Unable to reach the system after 7 Timeouts! Email notification Sent.", EventLogEntryType.FailureAudit); 

        // EMAIL NOTIFICATION 


        downCounter = 0; 
       } 
       else 
       { 
        sysDown = false; 

       } 

      } 
      catch (Exception ex) 
      { 
       //EXCEPTION HANDLING 

      } 
      loopCounter++; 

      if(sysDown==false && loopCounter>2) 
      { 
       eventLog.WriteEntry("The Tool Is Up Email Notification Sent", EventLogEntryType.SuccessAudit); 

       // EMAIL NOTIFICATION 

       loopCounter = 0; 
      } 

     } 

我所试图实现的是,在Ping超时7 =>时间(s),并发送电子邮件通知,说明它已关闭。如果系统在接下来的两次执行过程中启动,请发送一封电子邮件,说明系统已启动(我的代码每两次执行一次会发送一封电子邮件,说明系统已启动)。

这是如何实现的?

更新1:我有电子邮件逻辑。

更新2:Vibhav Ramcharan的解决方案触发系统在每次执行startMethod()时发出通知。

系统关闭通知的阈值为70%,这是单次执行期间连续7次ping故障。

假设系统停机。触发一封电子邮件,通知系统故障。

当系统启动时,执行成功执行两次。发送电子邮件通知系统已启动。

上面的代码会触发每个methodStart()上的系统电子邮件。最终,垃圾邮件。

回答

0

下面的代码工作,并做了必要的。

public void methodStart(object sender, ElapsedEventArgs e) 
      { 
       Ping p = new Ping(); 
       PingReply r; 
       string s = "SYSTEM-IP-ADDRESS"; 
       try 
       { 
        for (int i = 0; i < 10; i++) 
        { 
         r = p.Send(s); 
         if (r.Status == IPStatus.Success) 
         { 
          SuccessNoti(); 

         } 
         else 
         { 

          UnsuccessfulNoti(); 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        UnsuccessfulNoti(); 
       } 

       } 

      } 

      public void SuccessNoti() 
      { 
       if ((string.Compare(status, "Down", false) == 0) && Successcount >= 7) 
       { 
        using (MailMessage mail = new MailMessage()) 
        { 
         using (SmtpClient SmtpServer = new SmtpClient(smtp)) 
         { 
         // EMAIL NOTIFICATION 

          Successcount = 0; 
          status = null; 
         } 
        } 
       } 
       else 
       { 
        if (string.Compare(status, "Down", false) == 0) 
        { 
         Successcount = Successcount + 1; 
        } 
       } 
      } 

      public void sendfailureNotofication() 
      { 
       if (failureCount >= 7 && !(string.Compare(status, "Down", false) == 0)) 
       { 

        status = "Down"; 
        using (MailMessage mail = new MailMessage()) 
        { 
         using (SmtpClient SmtpServer = new SmtpClient(smtp)) 
         { 
         // EMAIL NOTIFICATION 

          failureCount = 0; 
          status = "Down"; 
         } 
        } 
       } 
       else 
       { 
        if (!(string.Compare(status, "Down", false) == 0)) 
        { 
         failureCount = failureCount + 1; 
        } 
       } 


      } 
0

好吧,阅读你的评论我认为你需要一个静态int在方法外声明。 例子:

class Program{ 

    private static int loopCounter = 0; 
    static void Main(string[] args) 
    { 
      // code 
    } 

    public void methodStart(object sender, ElapsedEventArgs e){ 
     // code 
    } 

} 

如果您需要在两次执行方法,你应该从methodStart提取所有的方法(例如名称:startUpMethod(),之后在methodStart调用startUpMethod()); 当你想再次调用该方法(不管它是否在同一个方法中),你再次调用startUpMethod()。 这被称为递归调用。 示例。

public void public void methodStart(object sender, ElapsedEventArgs e){ 
    startUpMethod(); 
} 

public void startUpMethod() 
{ 
     //do something 
     if(repeat) 
     startUpMethod() 
} 

你应该照顾无限循环

+0

不,我有电子邮件逻辑工作。我所追求的是在系统关闭后执行两次方法的逻辑。 – Tango

0

的从我的理解,你需要跟踪两个成功执行,并在发生故障后发送电子邮件。我在下面的代码中添加了评论。

protected override void OnStart(string[] args){ 

     var timer = new Timer 
     { 
      Enabled = true, 
      Interval = (10 * 60 * 1000) 
     }; 
     timer.Elapsed += new System.Timers.ElapsedEventHandler(methodStart); 
    } 

    private int loopCounter = 0; 
    bool sysDown = false; 

    // Used to count the number of successful executions after a failure. 
    int systemUpAfterFailureCount = 0; 

    public void methodStart(object sender, ElapsedEventArgs e) 
    { 

     Ping p = new Ping(); 
     PingReply r; 
     string s = "SYSTEM-IP-ADDRESS"; 
     int upCounter = 0; 
     int downCounter = 0; 


     try 
     { 
      for (int i = 0; i < 10; i++) 
      { 
       r = p.Send(s); 

       if (r.Status == IPStatus.Success) 
        upCounter++; 
       else 
        downCounter++; 
      } 

      if (downCounter >= 7) 
      { 
       // LOG ENTRY 
       // EMAIL NOTIFICATION 
       downCounter = 0; 
       // The system has failed 
       sysDown = true; 
      } 
      else 
      { 
       // Before changing the sysDown flag, check if the system was previously down 
       // This is the first successful execution after the failure 
       if (sysDown) 
        systemUpAfterFailureCount++; 

       // This will allow systemUpAfterFailureCount to increase if it is Ex: 1 and sysDown = false (Your success execution limit is 2, which we control at the IF block below) 
       if (systemUpAfterFailureCount > 1) 
        systemUpAfterFailureCount++; 

       sysDown = false; 
      } 

     } 
     catch (Exception ex) 
     { 
      //EXCEPTION HANDLING 
     } 

     loopCounter++; 

     // Check if the system is down, if it is up execute the following code for a maximum of 2 executions. 
     if (sysDown==false && systemUpAfterFailureCount <= 2) 
     { 
      // LOG ENTRY 
      loopCounter = 0; 

      // Send email for successful executions after a failure, limited to 2. 
      if (systemUpAfterFailureCount <= 2) 
      { 
       // EMAIL NOTIFICATION 
      } 

      // After two successful executions have occured, reset the counter 
      if (systemUpAfterFailureCount == 2) 
      { 
       systemUpAfterFailureCount = 0; 
      } 
     } 
    } 
+0

不幸的是,这个解决方案在每次执行startMethod后都会一直发送一封电子邮件。 – Tango