2012-06-08 41 views
0

我一直在ASP.NET MVC(MVC3)應用程序中掃描論壇以實現異常管理應用程序塊(EMAB)。使用MVC的異常管理應用程序塊

有很多談論ELMAH和NLOG的以及通過在Global.asax(http://s151.codeinspot.com/q/694875)vs,比ErrorController(http://www.davidjuth.com/asp-net-mvc-error-handler.aspx)的方式處理,以及利用下,[HandleError]裝修

我們正在研究規範我們使用EMAB管理我們的MVC應用程序的例外情況,但似乎無法找到提供全面解決方案的任何具體示例。

沒有人有任何聯繫或可以解釋究竟如何,你可能會在MVC框架

回答

1

我決定下面的方法中利用EMAB ...

// * *在Global.asax中

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception originalError = Server.GetLastError(); 
     Exception replacedError; 

     if (ExceptionPolicy.HandleException(originalError, "Global Policy", out replacedError)) 
     { 
      Exception ex = replacedError ?? originalError; 
      if (ex.InnerException != null) 
      { 
       ex = ex.InnerException; 
      } 
      var httpException = ex as HttpException; 
      HttpApplication httpApplication = this.Context.ApplicationInstance; 

      string message = Utility.GetFullMessage(httpApplication, ex, "::"); 
      string messageHtml = Utility.GetFullMessage(httpApplication, ex, "<br/>"); 

      LogEntry logEntry = new LogEntry(); 
      logEntry.EventId = 100; 
      logEntry.Priority = 2; 
      logEntry.Message = ex.Message + "::" + message; 
      Logger.Write(logEntry); 

      Response.Clear(); 
      Server.ClearError(); 

      var routeData = new RouteData(); 
      routeData.Values["controller"] = "Error"; 
      routeData.Values["action"] = Constants.ErrorGeneralKey; //"General" 
      routeData.Values["message"] = ex.Message; 
      routeData.Values["fullMessage"] = messageHtml; 
      Response.StatusCode = 500; 

      if (httpException != null) 
      { 
       Response.StatusCode = httpException.GetHttpCode(); 

       switch (Response.StatusCode) 
       { 
        case 403: 
         routeData.Values["action"] = "Http403"; 
         break; 

        case 404: 
         routeData.Values["action"] = "Http404"; 
         break; 

        default: 
         routeData.Values["httpStatusCode"] = Response.StatusCode; 
         break; 
       } 
      } 
      IController errorController = new ErrorController(); 
      var rc = new RequestContext(new HttpContextWrapper(Context), routeData); 
      errorController.Execute(rc); 
     } 
    } 

// * *在輔助類

public static string GetFullMessage(HttpApplication httpApplication, Exception ex, string delimiter) 
    { 
     return "StackTrace: " + ex.StackTrace 
        + delimiter + "User: " + ((httpApplication.User == null) ? "<null>" : httpApplication.User.Identity.Name) 
        + delimiter + "Data: " + GetExceptionData(ex.Data) 
        + delimiter + "Version: " + httpApplication.Request.Browser.Version 
        + delimiter + "Browser: " + httpApplication.Request.Browser.Browser 
        + delimiter + "Major Version: " + httpApplication.Request.Browser.MajorVersion.ToString() 
        + delimiter + "Minor Version: " + httpApplication.Request.Browser.MinorVersion.ToString() 
        + delimiter + "Javascript Version: " + httpApplication.Request.Browser.JScriptVersion.ToString() 
        + delimiter + "Ecma Script Version: " + httpApplication.Request.Browser.EcmaScriptVersion.ToString() 
        + delimiter + "Platform: " + httpApplication.Request.Browser.Platform 
        + delimiter + "Source: " + ex.Source 
        + delimiter + "Form: " + httpApplication.Request.Form.ToString() 
        + delimiter + "QueryString: " + httpApplication.Request.QueryString.ToString() 
        + delimiter + "TargetSite: " + ex.TargetSite; 
    } 

// * *在錯誤控制器

public ActionResult General(string httpStatusCode, string message, string fullMessage) 
    { 
     errorViewModel.RootCause = Enums.RootCause.General; 
     errorViewModel.HttpStatusCode = httpStatusCode; 
     errorViewModel.Message = message; 
     errorViewModel.FullMessage = fullMessage; 
     return View("Error", errorViewModel); 
    } 

    public ActionResult Http404() 
    { 
     errorViewModel.RootCause = Enums.RootCause.NotFound; 
     return View("Error", errorViewModel); 
    } 

    public ActionResult Http403() 
    { 
     errorViewModel.RootCause = Enums.RootCause.Forbidden; 
     return View("Error", errorViewModel); 
    } 
相關問題