【发布时间】:2018-10-14 01:22:12
【问题描述】:
我正在使用 ELMAH 尝试并记录处理的异常(发生在 try catch 中的异常)。但是,我无法让 ELMAH 记录在 try catch 中发生的任何异常。
这是我的行动:
[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);
}
}
我已经听从了这里的建议: https://docs.elmah.io/elmah-and-custom-errors/ 和这里:How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?
但我的ElmahExceptionLogger 只会因未处理的异常而被触发。
这是我的ElmahExceptionLogger:
public class ElmahExceptionLogger : IExceptionFilter {
public void OnException(ExceptionContext filterContext) {
if (filterContext.ExceptionHandled) {
ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
}
}
}
这是我的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);
}
}
这是我的注册全局过滤器方法:
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
});
}
}
有谁知道如何让 ELMAH 在尝试捕获中记录我的异常?
【问题讨论】:
标签: c# asp.net-mvc error-handling elmah