コード例 #1
0
ファイル: FileChannel.cs プロジェクト: rajeshkdev/FileChannel
        public bool SendNotification(BizTalkEnvironment environment, Alarm alarm, string globalProperties, Dictionary <MonitorGroupTypeName, MonitorGroupData> notifications)
        {
            //Construct Message
            string message = string.Empty;

            message += String.Format("\nAlarm Name: {0} \n\nAlarm Desc: {1} \n", alarm.Name, alarm.Description);
            message += "\n----------------------------------------------------------------------------------------------------\n";
            message += String.Format("\nEnvironment Name: {0} \n\nMgmt Sql Instance Name: {1} \nMgmt Sql Db Name: {2}\n", environment.Name, environment.MgmtSqlDbName, environment.MgmtSqlInstanceName);
            message += "\n----------------------------------------------------------------------------------------------------\n";

            // Change the Message Type and Format based on your need.
            BT360Helper helper = new BT360Helper(notifications, environment, alarm, MessageType.ConsolidatedMessage, MessageFormat.Text);

            message += helper.GetNotificationMessage();

            //Read configured properties
            XNamespace bsd   = XNamespace.Get("http://www.biztalk360.com/alarms/notification/basetypes");
            XNamespace prop  = XNamespace.Get("http://www.biztalk360.com/alarms/notification/properties");
            XNamespace xmlns = XNamespace.Get("http://www.biztalk360.com/alarms/notification/basetypes");

            string fileFolder = string.Empty; string folderOverride = string.Empty;

            //Alarm Properties
            XDocument almProps = XDocument.Load(new StringReader(alarm.AlarmProperties));

            foreach (var element in almProps.Descendants(bsd + "TextBox"))
            {
                if (element.Attribute("Name").Value == "file-override-path")
                {
                    folderOverride = element.Attribute("Value").Value;
                }
            }

            //Global Properties
            XDocument globalProps = XDocument.Load(new StringReader(globalProperties));

            foreach (var element in globalProps.Descendants(bsd + "TextBox"))
            {
                if (element.Attribute("Name").Value == "file-path")
                {
                    fileFolder = element.Attribute("Value").Value;
                }
            }

            //Save to Disk
            string filePath     = string.IsNullOrEmpty(folderOverride) ? fileFolder : folderOverride;
            string fileLocation = Path.Combine(filePath, Guid.NewGuid().ToString(), ".txt");

            using (FileStream fs = new FileStream(fileLocation, FileMode.CreateNew))
            {
                fs.Write(Encoding.UTF8.GetBytes(message), 0, message.Length);
            }

            return(true);
        }
コード例 #2
0
        public bool SendNotification(BizTalkEnvironment environment, Alarm alarm, string globalProperties, Dictionary <MonitorGroupTypeName, MonitorGroupData> notifications)
        {
            //Construct Message
            string message = string.Empty;

            message += String.Format("\nAlarm Name: {0} \n\nAlarm Desc: {1} \n", alarm.Name, alarm.Description);
            message += "\n----------------------------------------------------------------------------------------------------\n";
            message += String.Format("\nEnvironment Name: {0} \n\nMgmt Sql Instance Name: {1} \nMgmt Sql Db Name: {2}\n", environment.Name, environment.MgmtSqlDbName, environment.MgmtSqlInstanceName);
            message += "\n----------------------------------------------------------------------------------------------------\n";

            // Change the Message Type and Format based on your need.
            BT360Helper helper = new BT360Helper(notifications, environment, alarm, MessageType.ConsolidatedMessage, MessageFormat.Text);

            message += helper.GetNotificationMessage();

            //Read configured properties
            XNamespace bsd   = XNamespace.Get("http://www.biztalk360.com/alarms/notification/basetypes");
            XNamespace prop  = XNamespace.Get("http://www.biztalk360.com/alarms/notification/properties");
            XNamespace xmlns = XNamespace.Get("http://www.biztalk360.com/alarms/notification/basetypes");

            //Alarm Properties
            XDocument almProps = XDocument.Load(new StringReader(alarm.AlarmProperties));
            string    alarmName = string.Empty; string channelName = string.Empty;

            // To read the various Properties, change the Types Like -- CheckBox, DropDownBox
            foreach (var element in almProps.Descendants(bsd + "TextBox"))
            {
                // Provide Name of the Element
                if (element.Attribute("Name").Value == "alarm-Name")
                {
                    alarmName = element.Attribute("Value").Value;
                }
            }

            //Global Properties
            XDocument globalProps = XDocument.Load(new StringReader(globalProperties));

            foreach (var element in globalProps.Descendants(bsd + "TextBox"))
            {
                // Provide Name of the Element
                if (element.Attribute("Name").Value == "channel-Name")
                {
                    channelName = element.Attribute("Value").Value;
                }
            }

            // Save / Send  -- Your Operation Goes here

            //Returns the status
            return(true);
        }
コード例 #3
0
ファイル: FileChannel.cs プロジェクト: radtek/FileChannel
        public bool SendNotification(BizTalkEnvironment environment, Alarm alarm, string globalProperties, Dictionary <MonitorGroupTypeName, MonitorGroupData> notifications)
        {
            try
            {
                //Construct Message
                var message = string.Empty;
                message += string.Format("\nAlarm Name: {0} \n\nAlarm Desc: {1} \n", alarm.Name, alarm.Description);
                message += "\n----------------------------------------------------------------------------------------------------\n";
                message += string.Format("\nEnvironment Name: {0} \n\nMgmt Sql Instance Name: {1} \nMgmt Sql Db Name: {2}\n", environment.Name, environment.MgmtSqlDbName, environment.MgmtSqlInstanceName);
                message += "\n----------------------------------------------------------------------------------------------------\n";

                // Change the Message Type and Format based on your need.
                var helper = new BT360Helper(notifications, environment, alarm, MessageType.ConsolidatedMessage, MessageFormat.Text);
                message += helper.GetNotificationMessage();

                //Read configured properties
                var bsd = XNamespace.Get("http://www.biztalk360.com/alarms/notification/basetypes");

                var fileFolder = string.Empty; var folderOverride = string.Empty;

                //Alarm Properties
                var almProps = XDocument.Load(new StringReader(alarm.AlarmProperties));
                foreach (var element in almProps.Descendants(bsd + "TextBox"))
                {
                    var xAttribute = element.Attribute("Name");
                    if (xAttribute == null || xAttribute.Value != "file-override-path")
                    {
                        continue;
                    }
                    var attribute = element.Attribute("Value");
                    if (attribute != null)
                    {
                        folderOverride = attribute.Value;
                    }
                }

                //Global Properties
                var globalProps = XDocument.Load(new StringReader(globalProperties));
                foreach (var element in globalProps.Descendants(bsd + "TextBox"))
                {
                    var xAttribute = element.Attribute("Name");
                    if (xAttribute == null || xAttribute.Value != "file-path")
                    {
                        continue;
                    }
                    var attribute = element.Attribute("Value");
                    if (attribute != null)
                    {
                        fileFolder = attribute.Value;
                    }
                }

                //Save to Disk
                var filePath     = string.IsNullOrEmpty(folderOverride) ? fileFolder : folderOverride;
                var fileLocation = Path.Combine(filePath, Guid.NewGuid() + ".txt");
                using (var fs = new FileStream(fileLocation, FileMode.CreateNew))
                {
                    fs.Write(Encoding.UTF8.GetBytes(message), 0, message.Length);
                }
                LoggingHelper.Info("File notification completed successfully");
                return(true);
            }
            catch (Exception ex)
            {
                LoggingHelper.Info("File notification failed. Error " + ex.Message);
                return(false);
                // ignored
            }
        }
コード例 #4
0
        /// <summary>
        /// Sends the notification.
        /// </summary>
        /// <param name="environment">The environment.</param>
        /// <param name="alarm">The alarm.</param>
        /// <param name="globalProperties">The global properties.</param>
        /// <param name="notifications">The notifications.</param>
        /// <returns></returns>
        public bool SendNotification(BizTalkEnvironment environment, Common.Alarm alarm, string globalProperties, Dictionary <MonitorGroupTypeName, MonitorGroupData> notifications)
        {
            try
            {
                LoggingHelper.Info("DESCRIPTION " + alarm.Description);
                XDocument globalDocument = XDocument.Parse(globalProperties);
                XDocument alarmDocument  = XDocument.Parse(alarm.AlarmProperties);

                //Read configured properties
                XNamespace bsd              = XNamespace.Get("http://www.biztalk360.com/alarms/notification/basetypes");
                string     email            = string.Empty;
                string     emailTo          = string.Empty;
                string     ccEmail          = string.Empty;
                string     upAlert          = string.Empty;
                string     autoCorrectAlert = string.Empty;

                //Global Properties
                XDocument globalProps = XDocument.Load(new StringReader(globalProperties));

                foreach (XElement element in globalProps.Descendants(bsd + "TextArea"))
                {
                    XAttribute xAttribute = element.Attribute("Name");
                    if (element.Attribute("Name").Value == "Email-To")
                    {
                        emailTo = element.Attribute("Value").Value;
                    }
                    if (element.Attribute("Name").Value == "C-C")
                    {
                        ccEmail = element.Attribute("Value").Value;
                    }
                    if (element.Attribute("Name").Value == "Up-Alert")
                    {
                        upAlert = element.Attribute("Value").Value;
                    }
                    if (element.Attribute("Name").Value == "AutoCorrect-Alert")
                    {
                        autoCorrectAlert = element.Attribute("Value").Value;
                    }
                }
                bool useEmailTo = Convert.ToBoolean(globalDocument.XPathSelectElement(
                                                        "/*[local-name() = 'GlobalProperties']/*[local-name() = 'Section']/*[local-name() = 'CheckBox' and @Name = 'use-Email-To']")
                                                    ?.Attribute("Value")?.Value);

                BT360Helper helper = new BT360Helper(notifications, environment, alarm, MessageType.ConsolidatedMessage, MessageFormat.Text);

                EmailMessageBody emailMessageBody = (EmailMessageBody)helper.ConvertDictionaryToObject <EmailMessageBody>(alarm.AdditionalProperties, SMTPConfigurationInfo.EmailMessageBody);
                EmailAttachments emailAttachments = (EmailAttachments)helper.ConvertDictionaryToObject <EmailAttachments>(alarm.AdditionalProperties, SMTPConfigurationInfo.EmailAttachments);
                SMTPSetting      smtpSetting      = (SMTPSetting)helper.ConvertDictionaryToObject <SMTPSetting>(alarm.AdditionalProperties, SMTPConfigurationInfo.SMTPSetting);
                if (smtpSetting == null)
                {
                    throw new Exception("SMTP Setting is not present in the database. Please use the UI to update the correct setting.");
                }

                //When installed first time, these values will be blank
                if (smtpSetting.serverName == "" || smtpSetting.adminEmailAddress == "")
                {
                    //SK: 4th Feb 2012, replaced returning null with exception, since we can't proceed further.
                    throw new Exception("SMTP Setting is not configured (admin email or server name is blank). Please use the UI to update the correct setting.");
                    //return null;
                }
                switch (emailMessageBody.notificationType)
                {
                case NotificationType.UpAlert:
                    if (upAlert != "" && useEmailTo)
                    {
                        email = upAlert;
                    }
                    else
                    {
                        email = emailTo;
                    }
                    break;

                case NotificationType.AutoCorrectAlert:
                    if (autoCorrectAlert != "" && useEmailTo)
                    {
                        email = autoCorrectAlert;
                    }
                    else
                    {
                        email = emailTo;
                    }
                    break;

                default:
                    email = emailTo;
                    break;
                }

                string strFrom    = emailMessageBody.fromEmailAddress;
                string strName    = emailMessageBody.fromDisplayName;
                string strTo      = email;
                string strCC      = ccEmail;
                string strSubject = emailMessageBody.subject;
                string strBody    = emailMessageBody.xmlData;
                LoggingHelper.Info("SUBJECT " + strSubject);
                try
                {
                    SmtpClient smtp = new SmtpClient();

                    smtp.DeliveryMethod        = SmtpDeliveryMethod.Network;
                    smtp.Host                  = smtpSetting.serverName;
                    smtp.Port                  = Convert.ToInt32(smtpSetting.port);
                    smtp.EnableSsl             = (smtpSetting.sslMode == SMTPSSLMode.DedicatedSSLPort) ? true : false;
                    smtp.UseDefaultCredentials = (smtpSetting.authentication == SMTPAuthentication.IntegratedWindowsAuthOverNtlm) ? true : false;
                    smtp.Credentials           = (smtpSetting.authentication == SMTPAuthentication.Anonymous) ? null : new NetworkCredential(smtpSetting.userName, smtpSetting.password);

                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                    string      htmlData    = Helper.TransformXMLToHTML(emailMessageBody.xslt, emailMessageBody.xmlData);
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.From       = new MailAddress(strFrom, strName);
                    mailMessage.Subject    = strSubject;
                    mailMessage.Body       = htmlData;
                    mailMessage.IsBodyHtml = true;
                    //Adding multiple To Addresses
                    foreach (string sTo in strTo.Split(";".ToCharArray()))
                    {
                        mailMessage.To.Add(sTo);
                    }

                    //Adding multiple CC Addresses
                    if (strCC != string.Empty)
                    {
                        foreach (string sCC in strCC.Split(";".ToCharArray()))
                        {
                            mailMessage.CC.Add(sCC);
                        }
                    }
                    int    attachementCount = 0;
                    double totalSizeInMB    = 0;
                    var    attachmentsOrderedByImportance = emailAttachments.OrderByDescending(issue => issue.importance);

                    foreach (EmailAttachment attachment in attachmentsOrderedByImportance)
                    {
                        string fileName = Path.Combine(emailMessageBody.attachmentFolder, attachment.name);
                        if (File.Exists(fileName))
                        {
                            //Apply maximum size restriction rule
                            FileInfo f        = new FileInfo(fileName);
                            long     filesize = f.Length;
                            double   sizeInMB = (filesize / 1024f) / 1024f;

                            if ((totalSizeInMB + sizeInMB) >= emailMessageBody.maxAttachmentsSizeInMb)
                            {
                                LoggingHelper.Warn(string.Format("The size of the attachment is above the configured limit. Alarm {0}. FileName :{1}. Configured Value :{2}. Current Limit: {3}", alarm.Name, attachment.name, emailMessageBody.maxAttachmentsSizeInMb, totalSizeInMB));
                            }
                            else if (attachementCount >= emailMessageBody.maxAttachments)  //Apply Maximum attachments/email rule
                            {
                                LoggingHelper.Warn(string.Format("Maximum attachments count reached for alarm {0}. Configured Value :{1}. Current Limit: {2}", alarm.Name, emailMessageBody.maxAttachments, attachementCount));
                            }
                            else
                            {
                                mailMessage.Attachments.Add(new Attachment(fileName));
                                totalSizeInMB += sizeInMB; // Attachment Size
                                attachementCount++;        // Attachement Count
                            }
                        }
                        else
                        {
                            LoggingHelper.Warn(string.Format(" Attachment file {0} cannot be found.", attachment));
                        }
                    }


                    MailPriority mailPriority = MailPriority.Normal;
                    switch (alarm.EmailPriority)
                    {
                    case "1":
                        mailPriority = MailPriority.High;
                        break;

                    case "0":
                        mailPriority = MailPriority.Low;
                        break;
                    }
                    mailMessage.Priority = mailPriority;
                    smtp.Send(mailMessage);
                    LoggingHelper.Info("SMTP Notification channel posted the message successfully");
                    return(true);
                }
                catch (Exception ex)
                {
                    LoggingHelper.Error("SMTP Notification channel failed. Error " + ex.Message);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.Error("SMTP Notification channel failed. Error " + ex.Message);
                return(false);
            }
        }