2011-01-06 142 views
6

在我的MVC web項目。我正在嘗試向訪問者顯示自定義錯誤頁面,而不使用web.config中的「custromerrors」元素。Elmah沒有記錄異常

我能趕上的異常像下面

protected void Application_Error(object sender, EventArgs e) 
{ 

    Exception exception = Server.GetLastError(); 

    bool success = RaiseErrorSignal(exception); 

    Response.Clear(); 

    HttpException httpException = exception as HttpException; 

    RouteData routeData = new RouteData(); 
    routeData.Values.Add("controller", "Error"); 

    if (httpException == null) 
    { 
     routeData.Values.Add("action", "Index"); 
    } 
    else //It's an Http Exception, Let's handle it. 
    { 
     switch (httpException.GetHttpCode()) 
     { 
      case 404: 
       // Page not found. 
       routeData.Values.Add("action", "Error404"); 
       break; 
      case 500: 
       // Server error. 
       routeData.Values.Add("action", "Error500"); 
       break; 

      // Here you can handle Views to other error codes. 
      // I choose a General error template 
      default: 
       routeData.Values.Add("action", "Index"); 
       break; 
     } 
    } 

    // Pass exception details to the target error View. 
    routeData.Values.Add("error", exception); 

    // Clear the error on server. 
    Server.ClearError(); 

    // Call target Controller and pass the routeData. 
    IController errorController = new ProjectName.WebSite.Controllers.ErrorController(); 
    errorController.Execute(new RequestContext(
     new HttpContextWrapper(Context), routeData)); 


} 

private static bool RaiseErrorSignal(Exception e) 
{ 
    var context = HttpContext.Current; 
    if (context == null) 
     return false; 
    var signal = ErrorSignal.FromContext(context); 
    if (signal == null) 
     return false; 
    signal.Raise(e, context); 
    return true; 
} 

但是ELMAH着錯誤日誌還我養了錯誤信號。

+2

那麼,你的問題是什麼?我看到的只是一個結論。 – 2011-01-06 14:56:51

+0

signal.Raise(e,context)不起作用,因爲當我去/elmah.axd查看異常時,列表是空的。 – Yucel 2011-01-06 17:19:08

回答

9

我發現這個問題,我錯過了一個web.config部分。我將「ErrorLog」模塊添加到<system.webserver><modules>。我還需要將它添加到<system.web><httpModules>

添加後,Elmah開始記錄錯誤。

另外我不需要調用ErrorSignal.Raise()方法,Elmah可以在沒有信號的情況下檢測錯誤。