簡單屬性
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 is useful for retrieving info like controller, action or other route values
string controllerName = filterContext.RouteData.Values["controller"].ToString();
string actionName = filterContext.RouteData.Values["action"].ToString();
string exception = filterContext.Exception.ToString(); // Full exception stack
string message = filterContext.Exception.Message; // Message given by the exception
// Log the exception within database
LogExtensions.Insert(exception.ToString(), message, controllerName + "." + actionName);
base.OnException(filterContext);
}
}
}
然後在 FilterConfig.cs
中設定它
filters.Add(new CustomErrorHandlerFilter());