public void Intercept(IInvocation invocation) { if (!invocation.Method.IsPublic) {//必须为public方法,并且方法所在类实现标识接口IControllerIntercepted,才进行aop拦截,进行异常日志记录和审计日志 invocation.Proceed(); return; } try { this.ValidRequestParameter(invocation); DateTime beginTime = DateTime.Now; invocation.Proceed(); this.Audit(invocation, beginTime); } catch (InvokeFaildException gex) { if (gex.Method == null) { gex.Method = invocation.MethodInvocationTarget; } throw gex; } catch (Exception ex) { var gex = new InvokeFaildException(invocation.Arguments, "请查看内部异常详情", ex); gex.Method = invocation.MethodInvocationTarget; throw gex; } }
private ActionResult OnException(ExceptionContext filterContext, InvokeFaildException ex) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { return(this.Json(ApiResult.Error(ex.Message))); } else { return(this.Redirect("/home/error")); } }
private void ValidRequestParameter(IInvocation invocation) { var arguments = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(invocation.Arguments); var controller = invocation.Proxy as System.Web.Mvc.Controller; if (controller.ModelState.IsValid) { return; } string msg = string.Join(";", controller.ModelState.Values.SelectMany(x => x.Errors.Select(a => a.ErrorMessage)).ToList()); var gex = new InvokeFaildException(invocation.Arguments, msg); throw gex; }
protected override void OnException(ExceptionContext filterContext) { var gex = filterContext.Exception as InvokeFaildException; if (gex == null) { gex = new InvokeFaildException("未知", "发生未知异常", filterContext.Exception); } string eidName = gex.Method == null ? "未知" : gex.Method.DeclaringType.FullName + "." + gex.Method.Name; this.Logger.ErrorFormat("请求地址{0},控制器入参{1}", this.Request.Url.AbsolutePath, gex.RequestArgs); filterContext.Result = this.OnException(filterContext, gex); filterContext.ExceptionHandled = true; }