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

Dotnetnuke DNN 7中的自定义错误处理?,dotnetnuke,httpmodule,custom-error-handling,Dotnetnuke,Httpmodule,Custom Error Handling,这里有没有人有过DNN 7内部自定义错误处理的经验 内置日志记录很好,但我的公司需要扩展DNN的内置错误处理,以包括在出现所有异常时发送自定义电子邮件。我们创建了一个HttpModule,它在应用程序出错时添加了一个事件侦听器,并且一直以这种方式发送异常。但是,在通过电子邮件发送异常后,我们没有被一致地重定向到DNN属性中“管理”>“站点设置”下设置的指定500错误页面。根据异常类型的不同,我们有不同的行为。某些异常(NullReferenceException)会导致应用程序_错误触发并发送电

这里有没有人有过DNN 7内部自定义错误处理的经验

内置日志记录很好,但我的公司需要扩展DNN的内置错误处理,以包括在出现所有异常时发送自定义电子邮件。我们创建了一个HttpModule,它在应用程序出错时添加了一个事件侦听器,并且一直以这种方式发送异常。但是,在通过电子邮件发送异常后,我们没有被一致地重定向到DNN属性中“管理”>“站点设置”下设置的指定500错误页面。根据异常类型的不同,我们有不同的行为。某些异常(NullReferenceException)会导致应用程序_错误触发并发送电子邮件,但不会重定向,其他异常(HttpException)会导致重定向到500页而不会触发应用程序_错误事件。DNN中是否有可能在应用程序出错之前捕获到这些错误,以及如何修复这些问题的想法

以下是我们添加到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 } /// ///类,用于错误处理和向错误通讯组列表发送异常。 /// 公共类错误模块:IHttpModule{ #区域私有财产 /// ///获取请求的URL。 /// ///请求的URL。 私有字符串requestedUrl{ 得到{ //TODO:创建CmsPathTranslationFactory以创建ICmsPathTranslator对象以获取请求的URL路径 return!string.IsNullOrEmpty(HttpContext.Current.Items[“UrlRewrite:OriginalUrl”].ToString())? 新Uri(HttpContext.Current.Items[“UrlRewrite:OriginalUrl”].ToString()).AbsolutePath:HttpContext.Current.Request.Url.AbsolutePath; } } #端区 #区域IHTTP模块成员 /// ///初始化指定的应用程序。 /// ///应用程序。 public void Init(HttpApplication应用程序){ application.Error+=新事件处理程序(application\u Error); } /// ///处置由实现的模块使用的资源(内存除外)。 /// public void Dispose(){} #端区 #区域公共方法 /// ///处理应用程序控件的错误事件。 /// ///事件的来源。 ///包含事件数据的实例。 公共无效应用程序\u错误(对象发送方,事件参数e){ HttpApplication=(HttpApplication)发送方; //获取最后一个异常 Exception=application.Server.GetLastError(); if(异常==null){ //异常为null,没有要发送的内容 sendErrorMessage(新异常(“异常为空”); 返回; } //如果不为null,则获取内部异常 if(exception.InnerException!=null){ exception=exception.InnerException; } 如果(异常为HttpException&((HttpException)异常)。GetHttpCode()==404){ //不要为404发送异常电子邮件 } 否则{ sendErrorMessage(异常); } } #端区 #区域私有方法 /// ///发送包含指定错误的电子邮件。 /// ///例外。 私有void senderErrorMessage(异常示例){ 使用(MailMessage=newmailmessage()){ message.To.Add(新邮件地址(ConfigurationManager.AppSettings[“errorEmailToAddress”]); message.ReplyToList.Add(新邮件地址(ConfigurationManager.AppSettings[“errorEmailReplyToAddress”]); message.From=新邮件地址(ConfigurationManager.AppSettings[“errorEmailFromAddress”]、Environment.MachineName); message.Subject=getErrorEmailSubject(例如,requestedUrl); message.Body=ex.ToString(); message.Priority=MailPriority.High; 使用(SmtpClient=new SmtpClient()){ client.Host=ConfigurationManager.AppSettings[“SMTPServer”]; 客户端。发送(消息); } } } /// ///获取基于指定异常的错误电子邮件主题。 /// ///前任。 ///请求的URL路径。 ///System.String。 私有字符串getErrorEmailSubject(异常示例,字符串requestedPath){ return!string.IsNullOrEmpty(ex.Message)?string.Concat(requestedPath,“-”,ex.Message):requestedPath; } #端区 }
DNN已经可以针对每个异常发送电子邮件。您可以在事件日志中设置所有这些,编辑事件类型,并配置电子邮件选项。

我也有这个问题,问得好。感谢您指出这一点。我们尝试了这个功能,但它没有按我们想要的方式工作。我们需要更个性化的解决方案。