I am using ELMAH to try and log handled exceptions (ones that occur in try catches). However I am unable to get ELMAH to log any exceptions which occur in a try catch.
Here is my action:
[ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model) { try { throw new Exception("Log me elmah"); } catch (Exception e) { ModelState.AddModelError("", "Something unexpected happened, please try again."); return View(model); } } I have followed advice both from here: https://docs.elmah.io/elmah-and-custom-errors/ and here: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?
But my ElmahExceptionLogger is only getting triggered for unhandled exceptions.
Here is my ElmahExceptionLogger:
public class ElmahExceptionLogger : IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { ErrorSignal.FromCurrentContext().Raise(filterContext.Exception); } } } Here is my global.asax:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } Here is my register global filters method:
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ElmahExceptionLogger()); filters.Add(new HandleErrorAttribute()); /*Append no cache to all actions in all controllers so we don't cache any pages, I dont particularly like it because it means an increased server load However there are reasons to this; The first being data gets updated regularly and we want users to have the most up-to-date data And also you can press back after logging out to get to the cached page before. It can be overridden per action if needed */ filters.Add(new OutputCacheAttribute { VaryByParam = "*", Duration = 0, NoStore = true }); } } Does anyone know how to get ELMAH to log my exceptions in try catches?