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

这里的任何人都有DNN 7内部自定义错误处理的经验吗?

内置日志记录很好,但是我的公司需要扩展DNN的内置错误处理,以便在发生所有异常时包括发送自定义电子邮件。 我们创建了一个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

    }

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

链接地址: http://www.djcxy.com/p/7613.html

上一篇: Custom error handling in DNN 7?

下一篇: Array of textbox and labels how to get value in submit method in c#