2014-09-10 53 views
1

這裏的任何人都有DNN 7內部自定義錯誤處理的經驗嗎?DNN 7中的自定義錯誤處理?

內置的記錄是好的,但我的公司有需要延長DNN內建的錯誤處理包括當ALL發生異常發送定製的電子郵件。我們創建了一個HttpModule,它在Application_Error上添加了一個事件偵聽器,並以這種方式發送異常郵件。但是,在通過電子郵件發送異常之後,我們並未始終將其重定向到位於管理>網站設置下的DNN屬性中指定的500錯誤頁面。根據異常類型,我們有不同的行爲。一些異常(NullReferenceException)導致Application_Error觸發併發送一封電子郵件,但沒有重定向,其他(HttpException)導致重定向到500頁,而未觸發Application_Error事件。 DNN是否有可能在Application_Error觸發前捕獲這些錯誤,以及關於如何解決這些問題的任何想法?

這是我們加入到web.config中的HTTP模塊:

 

    /// 
    /// 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 

    } 
+0

我也有這個問題,很好的問題。 – bflemi3 2014-09-10 19:05:18

回答

0

DNN可以發送電子郵件,每例外了。您可以在事件日誌中設置所有這些,編輯事件類型以及配置電子郵件選項。

+0

感謝您指出這一點。我們嘗試了這個功能,但它沒有按照我們想要的方式工作。我們需要一個更加個性化的解決方案。 – 2014-09-11 20:01:34