DNN 7中的自定义错误处理?

时间:2014-09-10 18:49:27

标签: dotnetnuke httpmodule custom-error-handling

此处有人在DNN 7中有自定义错误处理经验吗?

内置日志记录很好,但我的公司需要扩展DNN的内置错误处理,以便在发生 ALL 异常时发送自定义电子邮件。我们创建了一个HttpModule,它在Application_Error上添加了一个事件监听器,并且一直通过电子邮件发送异常。但是,在通过电子邮件发送异常后,我们不会始终重定向到管理>下DNN属性中设置的指定500错误页面。网站设置。根据异常类型,我们有不同的行为。一些异常(NullReferenceException)导致Application_Error触发并且发送电子邮件但没有重定向,其他异常(HttpException)导致重定向到500页而没有触发Application_Error事件。是否有可能DNN中的某些内容在Application_Error触发之前捕获这些错误,以及有关如何解决这些问题的任何想法?

这是我们添加到web.config中的httpModule:


    /// 
    /// Class for error handling and emailing exceptions to the error distribution list.
    /// 
    public class ErrorModule : IHttpModule {

        #region Private Properties

        /// 
        /// Gets the requested URL.
        /// 
        /// The requested URL.
        private string requestedUrl {
            get {
                //TODO: create CmsPathTranslationFactory to create ICmsPathTranslator object for getting requested URL path
                return !string.IsNullOrEmpty(HttpContext.Current.Items["UrlRewrite:OriginalUrl"].ToString()) ?
                    new Uri(HttpContext.Current.Items["UrlRewrite:OriginalUrl"].ToString()).AbsolutePath : HttpContext.Current.Request.Url.AbsolutePath;
            }
        }

        #endregion

        #region IHttpModule Members

        /// 
        /// Initializes the specified application.
        /// 
        /// The application.
        public void Init(HttpApplication application) {
            application.Error += new EventHandler(application_Error);
        }

        /// 
        /// Disposes of the resources (other than memory) used by the module that implements .
        /// 
        public void Dispose() { }

        #endregion

        #region Public Methods

        /// 
        /// Handles the Error event of the application control.
        /// 
        /// The source of the event.
        /// The  instance containing the event data.
        public void application_Error(object sender, EventArgs e) {
            HttpApplication application = (HttpApplication)sender;

            // get the last exception
            Exception exception = application.Server.GetLastError();

            if (exception == null) {
                // exception is null, nothing to send
                sendErrorMessage(new Exception("Exception is null."));
                return;
            }

            // get the inner exception if not null
            if (exception.InnerException != null) {
                exception = exception.InnerException;
            }

            if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404) {
                //don't send exception email for 404
            }
            else {
                sendErrorMessage(exception);
            }
        }

        #endregion

        #region Private Methods

        /// 
        /// Sends an email message with the specified error.
        /// 
        /// The exception.
        private void sendErrorMessage(Exception ex) {
            using (MailMessage message = new MailMessage()) {
                message.To.Add(new MailAddress(ConfigurationManager.AppSettings["errorEmailToAddress"]));
                message.ReplyToList.Add(new MailAddress(ConfigurationManager.AppSettings["errorEmailReplyToAddress"]));
                message.From = new MailAddress(ConfigurationManager.AppSettings["errorEmailFromAddress"], Environment.MachineName);
                message.Subject = getErrorEmailSubject(ex, requestedUrl);
                message.Body = ex.ToString();
                message.Priority = MailPriority.High;

                using (SmtpClient client = new SmtpClient()) {
                    client.Host = ConfigurationManager.AppSettings["SMTPServer"];
                    client.Send(message);
                }
            }
        }

        /// 
        /// Gets the error email subject based on the specified exception.
        /// 
        /// The ex.
        /// The requested URL path.
        /// System.String.
        private string getErrorEmailSubject(Exception ex, string requestedPath) {
            return !string.IsNullOrEmpty(ex.Message) ? string.Concat(requestedPath, " - ", ex.Message) : requestedPath;
        }

        #endregion

    }

1 个答案:

答案 0 :(得分:0)

DNN可以为每个例外发送电子邮件。您可以在事件日志中设置所有这些,编辑事件类型和配置电子邮件选项。