Created
August 2, 2011 22:23
-
-
Save robertmilne/1121386 to your computer and use it in GitHub Desktop.
Elmah App_Start
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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