コード例 #1
0
        private static MFIAppLog log;//= NAppLoggerManager.GetLogger(typeof(NAppErrorFilter));
        #endregion

        #region 重写方法
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.IsNullOrEmpty())
            {
                throw new ArgumentNullException("filterContext is null");
            }

            var ex = filterContext.Exception as Exception;

            var refUrl = filterContext.HttpContext.Request.UrlReferrer.IsNullOrEmpty()
                             ? ""
                             : filterContext.HttpContext.Request.UrlReferrer.OriginalString;

            log = MFAppLoggerManager.GetLogger(filterContext.Controller.GetType());
            var errorMessage = "错误信息:{0}-Controller:{1}-Action:{2},源Url:{3}".FormatWith(ex.Message,
                                                                                        filterContext.RouteData.Values[
                                                                                            "controller"],
                                                                                        filterContext.RouteData.Values[
                                                                                            "action"], refUrl);

            log.Error(errorMessage, ex);

            filterContext.Controller.ViewData["ErrorMessage"] = ex.Message;

            filterContext.Result = new ViewResult()
            {
                ViewName = "Error", ViewData = filterContext.Controller.ViewData
            };

            filterContext.ExceptionHandled = true;

            filterContext.HttpContext.ClearError();
        }