public void OnActionExecuting(ActionExecutingContext filterContext) { if (!ShouldSaveAudit(filterContext)) { AprilAuditFilterData.Set(filterContext.HttpContext, null);//如果不需要审计则保存一个null return; } var auditInfo = _auditingHelper.CreateAuditInfo(filterContext.ActionDescriptor.ControllerDescriptor.ControllerType, filterContext.ActionDescriptor.GetMethodInfoOrNull(), filterContext.ActionParameters); var stopwatch = Stopwatch.StartNew(); AprilAuditFilterData.Set(filterContext.HttpContext, new AprilAuditFilterData(stopwatch, auditInfo));//将AprilAuditFilterData保存到httpContext.item中 }
public void OnActionExecuted(ActionExecutedContext filterContext) { var auditData = AprilAuditFilterData.GetOrNull(filterContext.HttpContext); //从httpContext中获取AprilAuditFilterData if (auditData == null) //如果为空则说明不需要审计 { return; } auditData.Stopwatch.Stop(); //更新auditInfo实体 auditData.AuditInfo.ExecutionDuration = Convert.ToInt32(auditData.Stopwatch.Elapsed.TotalMilliseconds); auditData.AuditInfo.Exception = filterContext.Exception; _auditingHelper.Save(auditData.AuditInfo);//保存 }
public static void Set(HttpContextBase httpContext, AprilAuditFilterData auditFilterData) { GetAuditDataStack(httpContext).Push(auditFilterData);//向stack中推入一个实体 }