using System;
using System.Web;
using System.Web.Mvc;
namespace Example.SDK.Filters
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class CustomErrorHandlerFilter : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
// RouteDate对于检索信息(例如控制器,操作或其他路由值)很有用
string controllerName = filterContext.RouteData.Values["controller"].ToString();
string actionName = filterContext.RouteData.Values["action"].ToString();
string exception = filterContext.Exception.ToString(); // 完整的异常堆栈
string message = filterContext.Exception.Message; // 异常给出的消息
// 在数据库中记录异常
LogExtensions.Insert(exception.ToString(), message, controllerName + "." + actionName);
base.OnException(filterContext);
}
}
}然后放入 FilterConfig.cs
filters.Add(new CustomErrorHandlerFilter());