Easiest best way to get Elmah going on MVC
Category: .NET C#
ELMAH is a great error logging app, that can be installed on any MVC app. I choose to use SQL compact so as not to bloat my main app's sql server DB with garbage and over XML because it otherwise you end up with a million files.
Step 1: Go to nuget, search elmah, and install package that says Elmah on SQL compact.
Step 2: Right click on project, "add deployable dependencies", check the box that says SQL compact. If this box is greyed, you need to install some extra stuff and VS tools.
Step 3: Secure elmah.axd to only admin roles in web.config
Step 4: Config. elmah section in web.config
Step 5: Global asax: add functions
// ELMAH Filtering
protected void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
FilterError404(e);
}
protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
FilterError404(e);
}
// Dismiss 404 errors for ELMAH
private void FilterError404(ExceptionFilterEventArgs e)
{
if (e.Exception.GetBaseException() is HttpException)
{
HttpException ex = (HttpException)e.Exception.GetBaseException();
if (ex.GetHttpCode() == 404)
e.Dismiss();
}
}
// seperate class
public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
// Long only handled exceptions, because all other will be caught by ELMAH anyway.
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
}
}
global asax intercept errors:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
}