2011-01-06 9 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

あなたの質問は何ですか?私が見ているのは結論だけです。 –

+0

signal.Raise(e、context)は動作しません。例外を見るために/elmah.axdに行くと、リストは空です。 – Yucel

答えて

9

問題が見つかりました。私はweb.configセクションを見逃しました。私は<system.webserver><modules>に "ErrorLog"モジュールを追加しました。

また、<system.web><httpModules>に追加する必要があります。

追加後、Elmahはエラーをログに記録します。

また、ErrorSignal.Raise()メソッドを呼び出す必要はありません。Elmahはシグナリングなしでエラーを検出できます。

+4

別の注記:私の場合は、App_Dataフォルダの書き込み権限が正しくありませんでした – devqon

関連する問題