private void SendNotify(ServiceNotifyEventArgs e, string emailSubject, string emailMessage, string smsMessage)
        {
            var preEmailMsg = (Debugger.IsAttached ? "<h3 style='color: #CD5C5C;'>Message sent in <strong>Developer</strong> mode</h3><br/>" : "")
                              + $"<h2>{ServiceLifeController.NewSetting.NotifyMessageContent}</h2><br/>";

            var postEmailMsg = $"<br/><br/><hr/><h5>Server Name is <strong>{Environment.MachineName}</strong></h5>";

            emailMessage = preEmailMsg + emailMessage + postEmailMsg;


            var preSmsMsg  = Debugger.IsAttached ? $"Message sent in [Developer] mode. {Environment.MachineName}" : "";
            var postSmsMsg = $"Server Name is [{Environment.MachineName}]";

            smsMessage = preSmsMsg + smsMessage + postSmsMsg;

            #region Send email to receivers
            try
            {
                //========================= Send Email to all of receivers ===================================
                if (ServiceLifeController.NewSetting.SendEmailNotifyEnable)
                {
                    var email = new EmailModel();

                    email.Message        = emailMessage;
                    email.From           = ServiceLifeController.NewSetting.SenderEmailAddress;
                    email.SenderPassword = ServiceLifeController.NewSetting.GetSenderEmailNoHashPassword();
                    email.Subject        = emailSubject;
                    email.To             = ServiceLifeController.NewSetting.ReceiverEmails;
                    email.EmailHost      = ServiceLifeController.NewSetting.EmailHost;
                    email.EmailHostPort  = ServiceLifeController.NewSetting.EmailHostPort;

                    email.SendEmail();
                }
                //============================================================================================
            }
            catch (Exception exp)
            {
                WindowsEventLog.WriteErrorLog($"Exception in ServiceLifeController_ServiceStatusChanged event (send email): {exp.Message}");
            }
            #endregion

            #region Send SMS to receivers
            try
            {
                //========================= Send SMS to all of receivers ===================================
                if (ServiceLifeController.NewSetting.SendSmsEnable)
                {
                    var results = new SmsManager.SmsHelper().SendManySms(
                        ServiceLifeController.NewSetting.SmsServiceUsername,
                        ServiceLifeController.NewSetting.GetSmsServiceNoHashPassword(),
                        smsMessage,
                        ServiceLifeController.NewSetting.SmsSenderNumber,
                        ServiceLifeController.NewSetting.SmsReceiverMobilesNo.ToArray());

                    for (var i = 0; i < results.Count; i++)
                    {
                        if (results[i].Retrieval == SmsManager.Retrieval.Successful)
                        {
                            WindowsEventLog.WriteInfoLog($"An SMS send to {results[i].ToNumber} by status: {results[i].Status} , recId: {results[i].RecId}");
                        }
                        else
                        {
                            WindowsEventLog.WriteWarningLog($"An SMS can't send to {results[i].ToNumber}, status: {results[i].Status} , recId: {results[i].RecId}");
                        }
                    }
                }
                //============================================================================================
            }
            catch (Exception exp)
            {
                WindowsEventLog.WriteErrorLog($"Exception in ServiceLifeController_ServiceStatusChanged event (send sms): {exp.Message}");
            }
            #endregion
        }
        private void ServiceLifeController_ServiceStatusChanged(ServiceNotifyEventArgs e)
        {
            #region Send Notify

            var emailMsg = $"<p>The <strong>{e.KeepService.Service.Name}</strong> process is <strong>{e.NewStatus}</strong>!</p>";

            var emailSubject = string.IsNullOrEmpty(ServiceLifeController.NewSetting.NotifyMessageTitle)
                        ? $"A Service is {e.NewStatus}!"
                        : ServiceLifeController.NewSetting.NotifyMessageTitle;

            var smsMsg = $@"{ServiceLifeController.NewSetting.NotifyMessageContent}{Environment.NewLine}" +
                         $"The <{e.KeepService.Service.Name}> process is [{e.NewStatus}]. {Environment.NewLine}";

            SendNotify(e, emailSubject, emailMsg, smsMsg);

            #endregion

            #region Keep Service Stablity Status

            try
            {
                // Keep service status on a stable state which is selected by user
                if (e.KeepService.KeepStatusOn != ServiceStableStatus.None)
                {
                    WindowsEventLog.WriteInfoLog($"Keep service status is ON for '{e.KeepService.Service.Name}'.");

                    if ((int)e.KeepService.KeepStatusOn != (int)e.NewStatus)
                    {
                        WindowsEventLog.WriteInfoLog(
                            $"Tying to keep '{e.KeepService.Service.Name}' status on {e.KeepService.KeepStatusOn} state...");
                        var result = false;
                        switch (e.KeepService.KeepStatusOn)
                        {
                        case ServiceStableStatus.Running:
                            result = ServiceInstallHelper.StartService(e.KeepService.Service.Name);
                            break;

                        case ServiceStableStatus.Stopped:
                            result = ServiceInstallHelper.StopService(e.KeepService.Service.Name);
                            break;

                        case ServiceStableStatus.Paused:
                            result = ServiceInstallHelper.PauseService(e.KeepService.Service.Name);
                            break;
                        }

                        if (result) // success
                        {
                            WindowsEventLog.WriteInfoLog($"The '{e.KeepService.Service.Name}' status rollbacked on {e.KeepService.KeepStatusOn} stable state successfull");

                            #region Send Notify

                            emailSubject = $"{e.KeepService.Service.Name} Rollbacked to {e.KeepService.KeepStatusOn} state Successfully";

                            emailMsg = $"<p style='color: #90EE90'>The <strong>{e.KeepService.Service.Name}</strong> process be rollbacked to <strong>{e.KeepService.KeepStatusOn}</strong> Successfully.</p>";

                            smsMsg = $@"Ok !{Environment.NewLine}" +
                                     $"The '{e.KeepService.Service.Name}' process be rollbacked to '{e.KeepService.KeepStatusOn}' Successfully.{Environment.NewLine}";

                            SendNotify(e, emailSubject, emailMsg, smsMsg);

                            #endregion
                        }
                        else // fail
                        {
                            WindowsEventLog.WriteWarningLog($"The '{e.KeepService.Service.Name}' status can not rollbacked, and state is still on {e.NewStatus} !!!");

                            #region Send Notify

                            emailSubject = $"{e.KeepService.Service.Name} Rollback to {e.KeepService.KeepStatusOn} state Failed!!";

                            emailMsg = $"<p style='color: #CD5C5C'>The <strong>{e.KeepService.Service.Name}</strong> process Rollbacking to <strong>{e.KeepService.KeepStatusOn}</strong> failed!!!</p>";

                            smsMsg = $@"Fail !!!{Environment.NewLine}" +
                                     $"The '{e.KeepService.Service.Name}' process can not rollbacked, and state is still on {e.NewStatus} !!!. {Environment.NewLine}";

                            SendNotify(e, emailSubject, emailMsg, smsMsg);

                            #endregion
                        }
                    }
                    else
                    {
                        WindowsEventLog.WriteInfoLog(
                            $"The '{e.KeepService.Service.Name}' process status is stable state({e.KeepService.KeepStatusOn}), Now.");
                    }
                }
                else
                {
                    WindowsEventLog.WriteInfoLog($"Keep service status is OFF for '{e.KeepService.Service.Name}' !!!");
                }
            }
            catch (Exception exp)
            {
                WindowsEventLog.WriteErrorLog($"Error in Keep service status on stable state by this Message: {exp.Message}");
            }

            #endregion
        }
 public static void OnServiceStatusChanged(ServiceNotifyEventArgs e)
 {
     ServiceStatusChanged?.Invoke(e);
     WindowsEventLog.WriteWarningLog($"The {e.KeepService.Service.Name} service is {e.NewStatus}!");
 }