Forked from lkaczanowski/CustomHandleErrorAttribute.cs
Created
September 12, 2017 11:08
-
-
Save dphoebus/730a22af622809ea592b785c910d217a to your computer and use it in GitHub Desktop.
Custom ASP.NET MVC handle error attribute. Handles and logs all errors.
This file contains hidden or 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
public class CustomHandleErrorAttribute : HandleErrorAttribute | |
{ | |
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
public override void OnException(ExceptionContext filterContext) | |
{ | |
if (!filterContext.HttpContext.IsCustomErrorEnabled) | |
{ | |
return; | |
} | |
if (!ExceptionType.IsInstanceOfType(filterContext.Exception)) | |
{ | |
return; | |
} | |
var controllerName = (string)filterContext.RouteData.Values["controller"]; | |
var actionName = (string)filterContext.RouteData.Values["action"]; | |
var 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 = (int)HttpStatusCode.InternalServerError; | |
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; | |
_log.Error("Internal server error occurred while handling web request.", filterContext.Exception); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment