예제 #1
0
        public static void SendNotification(string messageBody, string subject, string commaOrSemiColonSeparatedRecipientEmails)
        {
            if (!string.IsNullOrEmpty(notifyRecipients))
            {
                string[]    recipientList = commaOrSemiColonSeparatedRecipientEmails.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                MailMessage message       = new MailMessage();
                foreach (string recipient in recipientList)
                {
                    if (!recipient.Contains("@"))
                    {
                        Default.AddEntry("An invalid email address was specified", LogEventType.Warning);
                    }
                    else
                    {
                        message.To.Add(new MailAddress(recipient));
                    }
                }

                if (message.To.Count > 0)
                {
                    message.From       = new MailAddress(NotificationFrom);
                    message.Subject    = subject;
                    message.IsBodyHtml = true;
                    message.Body       = string.Format("<font face='arial'>{0}</font>", messageBody.Replace("\r\n", "<br />"));

                    SmtpClient client = new SmtpClient(smtpHost);
                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                        Default.AddEntry("An error occurred sending custom log message", ex);
                    }
                }
            }
        }
예제 #2
0
        private void Notify(string applicationName, LogEvent logEvent)
        {
            try
            {
                string csvList = string.Empty;
                switch (logEvent.Severity)
                {
                case LogEventType.None:
                    break;

                case LogEventType.Information:
                    csvList = NotifyOnInfo;
                    break;

                case LogEventType.Warning:
                    csvList = NotifyOnWarn;
                    break;

                case LogEventType.Error:
                    csvList = NotifyOnError;
                    break;

                case LogEventType.Fatal:
                    csvList = NotifyOnFatal;
                    break;

                default:
                    break;
                }
                if (!string.IsNullOrEmpty(csvList))
                {
                    string[] notifyList = csvList.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
                    if (notifyList.Length > 0)
                    {
                        MailMessage emailMessage = new MailMessage();
                        emailMessage.From       = new MailAddress(NotificationFrom);
                        emailMessage.Body       = CreateMailBody(applicationName, logEvent);
                        emailMessage.Subject    = string.Format("{0}: {1}", SubjectPrefix, logEvent.Severity.ToString());
                        emailMessage.IsBodyHtml = true;
                        SmtpClient client = new SmtpClient(SmtpHost);

                        foreach (string email in notifyList)
                        {
                            string emailAddr = email.Trim();
                            if (!emailAddr.Contains("@"))
                            {
                                Default.AddEntry("An invalid email address ({0}) was entered for {1} log notification.", new string[] { emailAddr, logEvent.Severity.ToString() });
                            }
                            else
                            {
                                emailMessage.To.Add(new MailAddress(emailAddr));
                            }
                        }

                        if (emailMessage.To.Count > 0)
                        {
                            try
                            {
                                client.Send(emailMessage);
                            }
                            catch (Exception ex)
                            {
                                Default.AddEntry("An error occurred sending {0} log notification.", ex, new string[] { logEvent.Severity.ToString() });
                            }
                        }
                    }
                }
            }
            catch //(Exception ex)
            {
                // keep the app from locking up or crashing if logging isn't configured
            }
        }