I cannot get Custom HandleErrorAttribute to be used in my MVC4 application.
A custom class that raises an exception
public class HandleAndLogErrorAttribute : HandleErrorAttribute { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public override void OnException(ExceptionContext filterContext) { var message = string.Format("Exception : {0}\n" + "InnerException: {1}", filterContext.Exception, filterContext.Exception.InnerException); Logger.Error(message); base.OnException(filterContext); } }
In Global.asax, I added a new static method:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleAndLogErrorAttribute());
When an exception is thrown, error.chstml is loaded, but the OnException override in the HandlErrorAttribute user attribute is never called.
I suspect that the RegisterGlobalFilters method in Global.asax is not called, and the exception there exception (see code) confirms this. This exception is never thrown.
In the web.config file, I set customErrors On:
<customErrors mode="On"> </customErrors>
I used the same approach in another solution, and I do not understand why this HandleErrorAttribute extension does not work.
source share