/// <summary> /// 在请求执行完后 记录请求的数据以及返回数据 /// </summary> /// <param name="actionExecutedContext"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { try { object watch = null; if (actionExecutedContext.Request.Properties.TryGetValue(StopwatchKey, out watch)) { string errorString = ""; //记录异常日志 if (actionExecutedContext.Exception != null) { errorString = JsonConvert.SerializeObject(actionExecutedContext.Exception); logger.Error(actionExecutedContext.Exception); } var stopwatch = (Stopwatch)actionExecutedContext.Request.Properties[StopwatchKey]; stopwatch.Stop(); var arguments = JsonUtil.ConvertArgumentsToJson(actionExecutedContext.ActionContext.ActionArguments); var s = actionExecutedContext.Request.Content.ReadAsStringAsync().Result; var httpContext = HttpContext.Current; var auditInfo = new AuditInfo() { Exception = errorString, MethodName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName, ExecutionTime = DateTime.Now, ExecutionDuration = stopwatch.ElapsedMilliseconds, Parameters = arguments, ServiceName = actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType .FullName, BrowserInfo = HttpContextExtension.GetBrowserInfo(httpContext), ClientIpAddress = HttpContextExtension.GetClientIpAddress(httpContext), ClientName = HttpContextExtension.GetComputerName(httpContext), Result = actionExecutedContext.Response != null ? actionExecutedContext.Response.Content.ReadAsStringAsync().Result : null }; SaveAuditInfo(auditInfo); } } catch { } base.OnActionExecuted(actionExecutedContext); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { try { if (filterContext.HttpContext.Items[StopwatchKey] != null) { var stopwatch = (Stopwatch)filterContext.HttpContext.Items[StopwatchKey]; stopwatch.Stop(); string errorString = ""; if (filterContext.Exception != null) { errorString = JsonConvert.SerializeObject(filterContext.Exception); logger.Error(filterContext.Exception); } string arguments = filterContext.HttpContext.Items[ArgumentsKey].ToString(); var httpContext = HttpContext.Current; var auditInfo = new AuditInfo() { Exception = errorString, MethodName = filterContext.ActionDescriptor.ActionName, ExecutionTime = DateTime.Now, ExecutionDuration = stopwatch.ElapsedMilliseconds, Parameters = arguments, ServiceName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.FullName, BrowserInfo = HttpContextExtension.GetBrowserInfo(httpContext), ClientIpAddress = HttpContextExtension.GetClientIpAddress(httpContext), ClientName = HttpContextExtension.GetComputerName(httpContext) }; SaveAuditInfo(auditInfo); } } catch { } }
/// <summary> /// 保存 /// </summary> /// <param name="entity"></param> public void SaveAuditInfo(AuditInfo entity) { auditInfoService.Save(entity); }
public void Save(AuditInfo auditInfo) { _auditInfoRepository.Insert(auditInfo); }
public Task SaveAsync(AuditInfo auditInfo) { logger.Info(auditInfo.ToString()); return Task.FromResult(0); }