Exemplo n.º 1
0
        public override void RecordMessage(AlertLevel alertLevel, string collectorType, string category, MonitorStates oldState, MonitorStates newState, CollectorMessage collectorMessage)
        {
            string lastStep = "";

            try
            {
                //rssWriteMutex.WaitOne();
                if (rssDocument == null)
                {
                    LoadRSSDocument();
                }
                string lineTitle = rssConfig.LineTitle
                                   .Replace("%DateTime%", DateTime.Now.ToString("F"))
                                   .Replace("%AlertLevel%", alertLevel.ToString())
                                   .Replace("%PreviousState%", oldState.ToString())
                                   .Replace("%CurrentState%", newState.ToString())
                                   .Replace("%CollectorName%", category)
                                   .Replace("%CollectorType%", collectorType);
                string lineCategory = rssConfig.LineCategory
                                      .Replace("%DateTime%", DateTime.Now.ToString("F"))
                                      .Replace("%AlertLevel%", alertLevel.ToString())
                                      .Replace("%PreviousState%", oldState.ToString())
                                      .Replace("%CurrentState%", newState.ToString())
                                      .Replace("%CollectorName%", category)
                                      .Replace("%CollectorType%", collectorType);
                string lineDetails = rssConfig.LineDescription
                                     .Replace("%Details%", collectorMessage.HtmlText)
                                     .Replace("%DateTime%", DateTime.Now.ToString("F"))
                                     .Replace("%AlertLevel%", alertLevel.ToString())
                                     .Replace("%PreviousState%", oldState.ToString())
                                     .Replace("%CurrentState%", newState.ToString())
                                     .Replace("%CollectorName%", category)
                                     .Replace("%CollectorType%", collectorType);

                rssDocument.Lines.Add(
                    new InMemoryRSSDocumentLine()
                {
                    Title   = lineTitle,
                    PubDate = DateTime.Now,
                    //GUID = Guid.NewGuid().ToString(),
                    Category    = lineCategory,
                    Description = lineDetails     //.Replace("\r\n", "<br/>")
                }
                    );
                rssDocument.SaveRssDocument();
            }
            catch (Exception ex)
            {
                throw new Exception("Error recording message in rss notifier\r\nLast step: " + lastStep, ex);
            }
            finally
            {
                //rssWriteMutex.ReleaseMutex();
            }
        }
Exemplo n.º 2
0
        public override void RecordMessage(AlertLevel alertLevel, string collectorType, string category, MonitorStates oldState, MonitorStates newState, CollectorMessage collectorMessage)
        {
            string lastStep = "";

            try
            {
                smtpWriteMutex.WaitOne();
                if (mailSettings.HostServer.Length == 0)
                {
                    throw new Exception("SMTP host server not specified!");
                }
                lastStep = "Setting up SMTP client details";
                using (SmtpClient smtpClient = new SmtpClient())
                {
                    smtpClient.Host = mailSettings.HostServer;
                    smtpClient.UseDefaultCredentials = mailSettings.UseDefaultCredentials;
                    smtpClient.Port = mailSettings.Port;

                    if (!mailSettings.UseDefaultCredentials)
                    {
                        lastStep = "Setting up non default credentials";
                        System.Net.NetworkCredential cr = new System.Net.NetworkCredential();
                        cr.Domain              = mailSettings.Domain;
                        cr.UserName            = mailSettings.UserName;
                        cr.Password            = mailSettings.Password;
                        smtpClient.Credentials = cr;
                    }
                    if (mailSettings.UseTLS)
                    {
                        smtpClient.EnableSsl = true;
                    }
                    MailMessage mailMessage = new MailMessage(mailSettings.FromAddress, mailSettings.ToAddress);

                    lastStep = "Setting up mail body";
                    string body = mailSettings.Body
                                  .Replace("%Details%", mailSettings.IsBodyHtml ? collectorMessage.HtmlText : collectorMessage.PlainText)
                                  .Replace("%DateTime%", DateTime.Now.ToString("F"))
                                  .Replace("%AlertLevel%", alertLevel.ToString())
                                  .Replace("%PreviousState%", oldState.ToString())
                                  .Replace("%CurrentState%", newState.ToString())
                                  .Replace("%CollectorName%", category)
                                  .Replace("%CollectorType%", collectorType);

                    string subject = mailSettings.Subject
                                     .Replace("%DateTime%", DateTime.Now.ToString("F"))
                                     .Replace("%AlertLevel%", alertLevel.ToString())
                                     .Replace("%PreviousState%", oldState.ToString())
                                     .Replace("%CurrentState%", newState.ToString())
                                     .Replace("%CollectorName%", category)
                                     .Replace("%CollectorType%", collectorType);

                    mailMessage.Priority = (MailPriority)mailSettings.MailPriority;
                    if (mailSettings.SenderAddress.Length > 0)
                    {
                        mailMessage.Sender = new MailAddress(mailSettings.SenderAddress);
                    }
                    if (mailSettings.ReplyToAddress.Length > 0)
                    {
                        mailMessage.ReplyToList.Add(mailSettings.ReplyToAddress);
                    }

                    mailMessage.Body       = body;
                    mailMessage.IsBodyHtml = mailSettings.IsBodyHtml;

                    mailMessage.Subject = subject;
                    lastStep            = "Sending SMTP mail";
                    smtpClient.Send(mailMessage);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error recording message in SMTP notifier\r\nLast step: " + lastStep, ex);
            }
            finally
            {
                smtpWriteMutex.ReleaseMutex();
            }
        }