Would like suggestions on quick and easy error handling for asp.net mvc web application.
Check the Nerd Dinner sample. It uses a very neat and simple approach.
ELMAH in conjunction with an attribute that extends HandleError and logs the errors through ELMAH. See this question/answer for some ideas on how to get the HandleError attribute to work with ELMAH. Dan Swatik also blogged about this with an implementation based on the accepted answer in that question.
I've just recently employed a very quick-n-dirty error reporting solution of my own that might give you some ideas...
Create a base controller class, lets call it MyBaseController
. Have all your controllers inherit from this if you like, though this isn't necessary if you only have one controller.
Then you can override its partial OnException
method and insert whatever kind of error reporting you might like, like to send yourself an email with the exception's ToString(). For example:
public MyOwnBaseController : Controller
protected override void OnException(ExceptionContext context)
{
SendAdminErrorAlert("Egads! An Error!", context.Exception.ToString());
// I have a view named "500.aspx" that I will now show...
context.ExceptionHandled = true;
this.View("500").ExecuteResult(this.ControllerContext);
}
}
I also found this article to be helpful: http://blog.dantup.me.uk/2009/04/aspnet-mvc-handleerror-attribute-custom.html in learning about all of this...
Good luck!
-f!
You can still use the old Application_Error method in the Global.asax
protected void Application_Error(object sender, EventArgs e)
{
// some good info in Server.GetLastError().GetBaseException() and Context.Request
// that you can use in whatever you choose for
// your quick and easy logging/reporting
}
Obviously, you'll want to turn on customErrors too...
<customErrors mode="RemoteOnly" defaultRedirect="~/error">
<error statusCode="403" redirect="~/error/forbidden" />
<error statusCode="404" redirect="~/error/notfound" />
<error statusCode="500" redirect="~/error" />
</customErrors>