static void LogServiceStatusChanged(ServicechangeEventArgs Orgs) { if (Orgs._ServiceChangeStatus.serviceStatus != ServiceControllerStatus.Running) { Console.WriteLine("Service Not Running "); // SentEmailOnServiceStopped(Orgs._ServiceChangeStatus.ServiceName, "Stopped"); } else { Console.WriteLine("Service Status Not Changed"); } }
public void CheckStatus(string ServiceName) { ServiceController sc = new ServiceController(ServiceName); var Service_ctrl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == ServiceName); //Console.WriteLine(Service_ctrl.Status); if (Service_ctrl != null) { if ((Service_ctrl.Status != ServiceControllerStatus.Running) && (OnServiceStatusChange != null)) { ServiceChangeStatus serviceChangeStatus = new ServiceChangeStatus(ServiceName, Service_ctrl.Status); ServicechangeEventArgs servicechangeEventArgs = new ServicechangeEventArgs(serviceChangeStatus); OnServiceStatusChange(servicechangeEventArgs); } } else { //Service not Available } }
static void SentEmailOnServiceStopped(ServicechangeEventArgs eventArgs) { if (eventArgs._ServiceChangeStatus.serviceStatus != ServiceControllerStatus.Running) { Console.WriteLine("Service Not Running "); using (var smtpClient = new SmtpClient()) { NetworkCredential networkCredential; smtpClient.Host = "smtp-mail.outlook.com"; smtpClient.Port = 587; smtpClient.UseDefaultCredentials = false; networkCredential = new NetworkCredential("*****@*****.**", "Tr@c345ucc355!!"); smtpClient.EnableSsl = true; smtpClient.Credentials = networkCredential; MailAddress frommailAddress = new MailAddress("*****@*****.**", "DELTA Team"); MailMessage mailMessage = new MailMessage() { SubjectEncoding = Encoding.UTF8, Body = eventArgs._ServiceChangeStatus.ServiceName + " Service is Not Running", BodyEncoding = Encoding.UTF8, IsBodyHtml = true }; mailMessage.From = frommailAddress; mailMessage.Subject = "DELTA 2.0 Health"; mailMessage.To.Add("*****@*****.**"); mailMessage.To.Add("*****@*****.**"); mailMessage.To.Add("*****@*****.**"); mailMessage.To.Add("*****@*****.**"); smtpClient.Send(mailMessage); } } else { Console.WriteLine("Service Status Not Changed"); } }