當前位置: 首頁>>代碼示例>>C#>>正文


C# ApplicationException.LogToElmah方法代碼示例

本文整理匯總了C#中System.ApplicationException.LogToElmah方法的典型用法代碼示例。如果您正苦於以下問題:C# ApplicationException.LogToElmah方法的具體用法?C# ApplicationException.LogToElmah怎麽用?C# ApplicationException.LogToElmah使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.ApplicationException的用法示例。


在下文中一共展示了ApplicationException.LogToElmah方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: SendMail

        /// <summary>
        /// Utility to send email.
        /// </summary>
        /// <param name="toEmailAddress">Email address of the receiver.</param>
        /// <param name="mailSubject">Email subject.</param>
        /// <param name="mailBody">Email body.</param>
        /// <param name="isHtml">Indicates value if the email is in HTML format or plain text.</param>
        /// <returns>Returns email send result.</returns>
        public static bool SendMail(string toEmailAddress, string mailSubject, string mailBody, bool isHtml = false)
        {
            var mailSendResult = false;
            try
            {
                var webConfiguration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                var mailSettingsSectionGroup = (MailSettingsSectionGroup)webConfiguration.GetSectionGroup(BllConstants.WebConfigurationMailSectionGroupNodeName);
                if (null != mailSettingsSectionGroup)
                {
                    var fromEmailAddress = mailSettingsSectionGroup.Smtp.Network.UserName;
                    if (!string.IsNullOrWhiteSpace(fromEmailAddress) && !string.IsNullOrWhiteSpace(toEmailAddress) && !string.IsNullOrWhiteSpace(mailSubject) && !string.IsNullOrWhiteSpace(mailBody))
                    {
                        using (var mailMessage = new MailMessage())
                        {
                            mailMessage.From = new MailAddress(fromEmailAddress);
                            toEmailAddress.Split(BllConstants.Semicolon).ToList().ForEach(mailMessage.To.Add);
                            mailMessage.Subject = mailSubject;
                            mailMessage.IsBodyHtml = isHtml;
                            mailMessage.Body = mailBody;
                            using (var smtpClient = new SmtpClient())
                            {
                                var mailConfigurationValid = true;
                                if (mailMessage.To.Any(mailAddress => mailAddress.Address.Equals(mailMessage.From.Address)))
                                {
                                    var smtpRelayHost = ConfigurationManager.AppSettings.Get(BllConstants.SmtpRelayHostAppConfigSettingKey);
                                    int smtpRelayPort;
                                    if (!string.IsNullOrEmpty(smtpRelayHost) && int.TryParse(ConfigurationManager.AppSettings.Get(BllConstants.SmtpRelayPortAppConfigSettingKey), out smtpRelayPort))
                                    {
                                        smtpClient.Host = smtpRelayHost;
                                        smtpClient.Port = smtpRelayPort;
                                        smtpClient.EnableSsl = bool.Parse(ConfigurationManager.AppSettings.Get(BllConstants.SmtpRelayEnableSslAppConfigSettingKey));
                                    }
                                    else
                                    {
                                        var applicationException = new ApplicationException(BllConstants.MailRelaySettingsNotFound);
                                        applicationException.LogToElmah();
                                        mailConfigurationValid = false;
                                    }
                                }

                                if (mailConfigurationValid)
                                {
                                    smtpClient.Send(mailMessage);
                                    mailSendResult = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    var applicationExcpetion = new ApplicationException(BllConstants.MailSettingsGroupNotFound);
                    applicationExcpetion.LogToElmah();
                }
            }
            catch (ArgumentException argumentException)
            {
                argumentException.LogToElmah();
            }
            catch (FormatException formatException)
            {
                formatException.LogToElmah();
            }
            catch (InvalidOperationException invalidOperationException)
            {
                invalidOperationException.LogToElmah();
            }
            catch (SmtpFailedRecipientsException smtpFailedRecipientsException)
            {
                smtpFailedRecipientsException.LogToElmah();
            }
            catch (SmtpException smtpException)
            {
                smtpException.LogToElmah();
            }

            return mailSendResult;
        }
開發者ID:zgying,項目名稱:CRMWeiXin,代碼行數:86,代碼來源:Messages.cs


注:本文中的System.ApplicationException.LogToElmah方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。