Easiest best way to get Elmah going on MVC

Posted on: Thu Jan 19 19:35:14 -0800 2012. Updated on: Thu Jan 19 19:46:20 -0800 2012.
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());
		}

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.