2017-07-12 39 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