MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获

public class BaseController : Controller
    { 
        /// <summary>
        /// Called after the action method is invoked.
        /// </summary>
        /// <param name="filterContext">Information about the current request and action.</param>
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {

            var servicename = string.Empty;

            foreach (var value in filterContext.RequestContext.RouteData.Values) 
            {
                if (value.Key.ToLower() == "controller")  //获取当前的Controller
                {
                    servicename += value.Value.ToString() + "---";
                }
                else if (value.Key.ToLower() == "action")//获取当前的ActionName
                {
                    servicename += value.Value.ToString();
                }
            }
            LogWriter.Debug(servicename);
            base.OnActionExecuted(filterContext);
        } 

        /// <summary>
        /// Called when an unhandled exception occurs in the action.
        /// </summary>
        /// <param name="filterContext">Information about the current request and action.</param>
        protected override void OnException(ExceptionContext filterContext)
        {
            LogWriter.Error(filterContext.Exception.Message, filterContext.Exception); //全局异常捕获输出

            base.OnException(filterContext);
        }

    }  

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。