グローバルHandleErrorAttribute
を使用するために多くのアドバイスを受けたいと思いますが、この属性はError
の表示にまっすぐに進み、例外を記録する場所はありません。私はビューがロギングやその他のビジネスロジックのための適切な場所だとは思わない。MVC 5アプリケーションで例外を記録する場所とタイミング
BaseController
にOnException
を上書きするオプションもありますが、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
}
を使用します。https://msdn.microsoft.com/en-us/library/system.web.httpapplication.error (v = vs.110).aspx –