一、自定义特性
/// <summary> /// 描述特性 /// </summary> [AttributeUsage(AttributeTargets.Method)] public sealed class DescribeAttribute : Attribute { private string _funDescribe; public DescribeAttribute() { } public DescribeAttribute(string funDescribe) { this._funDescribe = funDescribe; } public string FunDescribe { get { return _funDescribe; } } }
二、在Action上面加入特性
[Describe("系统主页面")] public ActionResult Index() { return View(); }
三、继承ActionFilterAttribute实现LogFilter日志
public sealed class LogFilter : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext == null) { throw new ArgumentNullException(nameof(filterContext)); } object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(DescribeAttribute), false); if (attrs.Length > 0) { string funDescribe = ((DescribeAttribute)attrs[0]).FunDescribe; } base.OnActionExecuted(filterContext); } }
四、在App_Start文件夹下面加入全局日志过滤器
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new AuthorizeAttribute()); filters.Add(new HandleErrorAttribute()); filters.Add(new LogFilter()); } }