Reputation: 51064
I would like to follow lots of advice to just use a global HandleErrorAttribute
, but this attribute seems to go straight to displaying the Error
view, leaving me no place to log the exception. I don't believe the view is the right place for logging and other business logic.
I also have the option of overriding OnException
in my BaseController
, but am worried about a conflict with HandleErrorAttribute
. If I override OnException
as follows, leaving the exception unhandled, will it carry on through to 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;
}
Or how else do I go about logging the exception? I believe the above two methods save me from implementing a 'try-catch' on every action method, and with catching specific exceptions this could be quite some chore. I would also like to have the option to restart the application or something should a really catastrophic exception occur, and again HandleErrorAttribute
doesn't leave much room for that.
Upvotes: 3
Views: 2105
Reputation: 1325
Usually, Global.asax is a good place to do that:
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
}
Upvotes: 3
Reputation: 11
Yes use Global.asax
protected void Application_Error(object sender, EventArgs e)
{
System.Web.HttpRuntime.UnloadAppDomain();
}
Upvotes: 1
Reputation: 6941
Check out StackExchange.Exceptional, an open source exception logger that is was created for and used by SO.
https://github.com/NickCraver/StackExchange.Exceptional
Upvotes: 0