Skip to content

Instantly share code, notes, and snippets.

@robertmilne
Created August 2, 2011 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmilne/1121386 to your computer and use it in GitHub Desktop.
Save robertmilne/1121386 to your computer and use it in GitHub Desktop.
Elmah App_Start
[assembly: WebActivator.PreApplicationStartMethod(typeof(YourNamespace.App_Start.ElmahPackage), "PreStart")]
namespace YourNamespace.App_Start
{
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Elmah;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
public static class ElmahPackage
{
public static void PreStart()
{
DynamicModuleUtility.RegisterModule(typeof(ErrorLogModule));
RouteTable.Routes.Add("elmah",
new Route(
"elmah/{*pathInfo}",
new RouteValueDictionary(new { controller = string.Empty, action = string.Empty }),
new ElmahRouteHandler()));
GlobalFilters.Filters.Add(new ElmahHandleErrorAttribute());
}
}
public class ElmahRouteHandler : IRouteHandler
{
private static ErrorLogPageFactory factory = new ErrorLogPageFactory();
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var application = (HttpApplication)requestContext.HttpContext.GetService(typeof(HttpApplication));
var pathInfo = string.Concat("/", requestContext.RouteData.Values["pathInfo"]);
application.Context.RewritePath("/elmah", pathInfo, application.Request.QueryString.ToString(), false);
return factory.GetHandler(application.Context, null, null, null);
}
}
public class ElmahHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);
if (context.ExceptionHandled)
RaiseErrorSignal(context.Exception);
}
private static void RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
ErrorSignal.FromContext(context).Raise(e, context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment