Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 4, 2013 12:04
Show Gist options
  • Select an option

  • Save ChrisMcKee/4452134 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4452134 to your computer and use it in GitHub Desktop.
using System.Web.Mvc;
using Elmah;
public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
// Log only handled exceptions, because all other will be caught by ELMAH anyway.
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
}
}
/*
Goes in the register global filters in MVC4
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new MyHandleErrorAttribute());
}
The handle error attribute below allows view to be specified and is used ala
[MyHandleError(ExceptionType = typeof (ValidProductCouldNotBeFound), View = "ProductsException")]
*/
namespace Helpers
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Web;
using System.Web.Mvc;
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "This attribute is AllowMultiple = true and users might want to override behavior.")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class MyHandleErrorAttribute : FilterAttribute, IExceptionFilter
{
private const string DefaultView = "Error";
private readonly object _typeId = new object();
private Type _exceptionType = typeof (Exception);
private string _master;
private string _view;
public Type ExceptionType
{
get { return _exceptionType; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (!typeof (Exception).IsAssignableFrom(value))
{
throw new ArgumentException(value.FullName);
}
_exceptionType = value;
}
}
public string Master
{
get { return _master ?? String.Empty; }
set { _master = value; }
}
public override object TypeId
{
get { return _typeId; }
}
public string View
{
get { return (!String.IsNullOrEmpty(_view)) ? _view : DefaultView; }
set { _view = value; }
}
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
}
// If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
Exception exception = filterContext.Exception;
// If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != 500)
{
return;
}
if (!ExceptionType.IsInstanceOfType(exception))
{
return;
}
string controllerName = (string) filterContext.RouteData.Values["controller"];
string actionName = (string) filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
// Certain versions of IIS will sometimes use their own error page when
// they detect a server error. Setting this property indicates that we
// want it to try to render ASP.NET MVC's error page instead.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment