我已经写了下面的过滤器来记录和处理我的MVC 5应用程序中的异常:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class HandleAndLogErrorAttribute : HandleErrorAttribute {
public override void OnException(ExceptionContext filterContext) {
if (filterContext == null)
throw new ArgumentNullException("filterContext");
if (filterContext.IsChildAction) {
LogChildActionException(filterContext);
return;
}
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
return;
var exception = filterContext.Exception;
if (!ExceptionType.IsInstanceOfType(exception))
return;
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
LogActionException(controllerName, actionName, exception);
if (new HttpException(null, exception).GetHttpCode() != 500)
return;
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
var exceptionContext = filterContext;
var viewResult = new ViewResult {
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
exceptionContext.Result = viewResult;
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
private void LogActionException(string controllerName, string actionName, Exception exception) {
// log here
}
private void LogChildActionException(ExceptionContext filterContext) {
// log here
}
}
我添加此过滤器而不是原始过滤器:
public class FilterConfig {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new HandleAndLogErrorAttribute ());
}
}
但它似乎根本没有影响:
public class TestController : Controller {
// I even tried this line:
// [HandleAndLogErrorAttribute]
// and also this one:
// [HandleAndLogErrorAttribute(ExceptionType = typeof(Exception))]
public ActionResult Index() {
try {
var i = new[] { 1, 2, 3, 4 };
var j = i[10];
} catch {
throw new HttpException();
// I also tried:
// throw new Exception();
// and
// throw;
}
return null;
}
}
我确实在OnException()
方法上设置了一个断点。但它永远不会被召唤。你知道那里发生了什么吗?我错过了什么?提前谢谢。
这是我的其他配置:
Global.asax.cs
public class MvcApplication : System.Web.HttpApplication {
void Application_Error(object sender, EventArgs e) {
// I tried this line too:
//Server.ClearError();
// but nothing changed!
}
}
web.config
<system.web>
<!-- I also tried false -->
<trace enabled="true"/>
<!-- I also tried Off -->
<customErrors mode="On" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="/404.html"/>
</customErrors>
<!-- I also tried true -->
<compilation debug="false" targetFramework="4.5"/>
</system.web>
<system.webServer>
<!-- I also tried Detailed -->
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/404.html" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
当然,我尝试在启用/不启用调试和调试/发布模式的情况下运行应用程序。