標籤:
應用情境:IIS網站掛掉,系統自動重啟IIS,通知相關連絡人;
主要代碼:
監控類
public class monitoringiis { EmailSend send = new EmailSend(); public void MonitoringISSAppPool(object obj) { string method_Start = "Start"; string ISSAppPoolName = ""; string iislist = System.Configuration.ConfigurationManager.AppSettings["iislist"]; DirectoryEntry appPool = new DirectoryEntry("IIS://LOCALHOST/W3SVC/AppPools"); while (true) { try { foreach (DirectoryEntry item in appPool.Children) { string AppPoolCommand = item.Properties["ManagedPipelineMode"].Value.ToString(); string ManagedRuntimeVersion = item.Properties["ManagedRuntimeVersion"].Value.ToString();//,net版本號碼 string AppPoolState = item.Properties["AppPoolState"].Value.ToString();//目前狀態 if (AppPoolState != "2" && iislist.Contains(item.Name.Trim())) { DirectoryEntry findPool = appPool.Children.Find(item.Name, "IIsApplicationPool"); findPool.Invoke(method_Start, null); ISSAppPoolName = item.Name; appPool.CommitChanges(); appPool.Close(); var IIsMsg = string.Format("應用池名稱:[{0}],NET版本:{1},標識:{2},目前狀態:[{3}],時間:{4}/n", item.Name, ManagedRuntimeVersion, item.SchemaClassName, "已啟動", DateTime.Now.ToString()); logclass.Info(IIsMsg); } } Thread.Sleep(180000); } catch (Exception ex) { logclass.Error("MonitoringISSAppPool:" + ISSAppPoolName + string.Format("==時間:{0},啟動失敗", DateTime.Now) + "==" + ex.Source + "==" + ex.InnerException + "==" + ex.TargetSite + "==" + ex.Message);//日誌記錄錯誤資訊 send.SendMail(string.Format("伺服器:" + GetComputerInfo() + "==MonitoringISSAppPool:{0},時間:{1},啟動失敗", ISSAppPoolName, DateTime.Now) + "==" + ex.Message, ISSAppPoolName + "ISS服務發生問題,請儘快查看原因並解決");//郵件發送錯誤資訊 Thread.Sleep(10000); } } } /// <summary>擷取當前電腦資訊 例如:電腦名稱:aaa,IP:10.10.10.10</summary> /// <returns></returns> public static string GetComputerInfo() { var computerNmae = System.Net.Dns.GetHostName();//擷取當前電腦名稱 var computerIp = new System.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address).ToString(); //擷取區域網路IP return string.Format("PC_Name: {0},IP: {1}", computerNmae, computerIp); } }
View Code
發郵件類:
public class EmailSend { public void SendMail(string sendmessage,string sendtitle) { string senduser = System.Configuration.ConfigurationManager.AppSettings["senduser"]; string sendpwd = System.Configuration.ConfigurationManager.AppSettings["sendpwd"]; string[] receiveusers = System.Configuration.ConfigurationManager.AppSettings["receiveusers"].Split(‘|‘); string[] fasong = senduser.Split(‘@‘); string[] fs = fasong[1].Split(‘.‘); //發送 SmtpClient client = new SmtpClient("smtp.163.com"); //設定郵件協議 client.UseDefaultCredentials = false;//這一句得寫前面 client.DeliveryMethod = SmtpDeliveryMethod.Network; //通過網路發送到Smtp伺服器 client.Credentials = new NetworkCredential(fasong[0].ToString(), sendpwd); //通過使用者名稱和密碼 認證 foreach (string jsuser in receiveusers) { MailMessage mmsg = new MailMessage(new MailAddress(senduser), new MailAddress(jsuser)); //寄件者和收件者的郵箱地址 mmsg.Subject = sendtitle; //郵件主題 mmsg.SubjectEncoding = Encoding.UTF8; //主題編碼 mmsg.Body = sendmessage; //郵件內文 mmsg.BodyEncoding = Encoding.UTF8; //本文編碼 mmsg.IsBodyHtml = true; //設定為HTML格式 mmsg.Priority = MailPriority.High; //優先順序 try { client.Send(mmsg); } catch (Exception ex) { logclass.Debug("發送郵件失敗:" + ex.Message); } } } }
View Code
C#背景程式重啟IIS,發郵件通知