2016-05-16 8 views
3

グローバルHandleErrorAttributeを使用するために多くのアドバイスを受けたいと思いますが、この属性はErrorの表示にまっすぐに進み、例外を記録する場所はありません。私はビューがロギングやその他のビジネスロジックのための適切な場所だとは思わない。MVC 5アプリケーションで例外を記録する場所とタイミング

BaseControllerOnExceptionを上書きするオプションもありますが、HandleErrorAttributeとの競合が心配です。 OnExceptionを次のように上書きし、例外を処理しないままにした場合、それはHandleErrorAttributeまで続きますか?

protected override void OnException(ExceptionContext filterContext) 
{ 
    Exception exception = filterContext.Exception; 
    var controller = filterContext.RouteData.Values["controller"].ToString(); 
    var action = filterContext.RouteData.Values["action"].ToString(); 
    logger.Error(exception, "Exception in controller '{0}': action '{1}'.", controller, action); 
    filterContext.Exception = exception; 
    //filterContext.ExceptionHandled = true; 
} 

他にどのように例外をログに記録するのですか?私は、上記の2つの方法は、私がすべてのアクションメソッドに「try-catch」を実装するのを免れていると確信しています。私はまた、アプリケーションを再起動するか、何かが本当に致命的な例外が発生するかどうかを確認したいと思います。また、HandleErrorAttributeには余裕がありません。

protected void Application_Error(object sender, EventArgs e) 
{ 
    // Get the exception object. 
    Exception exception = Server.GetLastError(); 

    // Handle Exception 

    // Call Server.ClearError() if the exception is properly handled 
} 

答えて

0

チェックアウトStackExchange.Exceptional、のために作成され、SOによって使用されたされたオープンソースの例外ロガー:

1

はい、そのためのドキュメントはここにあるのGlobal.asax

protected void Application_Error(object sender, EventArgs e) 
{ 
    System.Web.HttpRuntime.UnloadAppDomain(); 
} 
+0

アプリをシャットダウンするのは例外的なケースでした。私の質問の要点は、例外ログをどこで行うかということでした。しかし、ありがとう、私はシャットダウンを行う方法を知らなかった。 – ProfK

関連する問題