public Task SaveAsync(AuditInfo auditInfo) { if (auditInfo.Exception == null) { Logger.Information(auditInfo.ToString()); } else { Logger.Information(auditInfo.ToString()); } return(Task.FromResult(0)); }
public Task SaveAsync(AuditInfo auditInfo) { if (auditInfo.Exception == null) { //Logger.InfoFormat("CustomData: {0}", auditInfo.CustomData); //Logger.InfoFormat("Parameters {0}", auditInfo.Parameters); //Logger.InfoFormat("ServiceName {0}", auditInfo.ServiceName); Logger.Info(auditInfo.ToString()); } else { Logger.Warn(auditInfo.ToString()); } return(Task.FromResult(0)); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!ShouldSaveAudit(context)) { await next(); return; } var type = (context.ActionDescriptor as ControllerActionDescriptor).ControllerTypeInfo.AsType(); var method = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo; var arguments = context.ActionArguments; var stopwatch = Stopwatch.StartNew(); var auditInfo = new AuditInfo { UserId = _admSession?.UserId, UserName = _admSession?.Name, ServiceName = type != null ? type.FullName.TruncateWithPostfix(EntityDefault.FieldsLength250) : "", //5.0版本以上,varchar(50),指的是50字符,无论存放的是数字、字母还是UTF8汉字(每个汉字3字节),都可以存放50个。其他数据库要注意下这里 MethodName = method.Name.TruncateWithPostfix(EntityDefault.FieldsLength250), Parameters = ConvertArgumentsToJson(arguments).TruncateWithPostfix(EntityDefault.FieldsLength2000), ExecutionTime = DateTime.Now, BrowserInfo = _clientInfoProvider.BrowserInfo.TruncateWithPostfix(EntityDefault.FieldsLength250), ClientIpAddress = _clientInfoProvider.ClientIpAddress.TruncateWithPostfix(EntityDefault.FieldsLength50), ClientName = _clientInfoProvider.ComputerName.TruncateWithPostfix(EntityDefault.FieldsLength100), }; ActionExecutedContext result = null; try { result = await next(); if (result.Exception != null && !result.ExceptionHandled) { auditInfo.Exception = result.Exception; } } catch (Exception ex) { auditInfo.Exception = ex; throw; } finally { stopwatch.Stop(); auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); if (result != null) { switch (result.Result) { case ObjectResult objectResult: auditInfo.ReturnValue = JsonConvert.SerializeObject(objectResult.Value); break; case JsonResult jsonResult: auditInfo.ReturnValue = JsonConvert.SerializeObject(jsonResult.Value); break; case ContentResult contentResult: auditInfo.ReturnValue = contentResult.Content; break; } } Console.WriteLine(auditInfo.ToString()); auditInfo.ReturnValue = auditInfo.ReturnValue.TruncateWithPostfix(EntityDefault.FieldsLength20); await _auditLogService.SaveAsync(auditInfo); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { // 判断是否写日志 if (!ShouldSaveAudit(context)) { await next(); return; } //接口Type var type = (context.ActionDescriptor as ControllerActionDescriptor).ControllerTypeInfo.AsType(); //方法信息 var method = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo; //方法参数 var arguments = context.ActionArguments; //开始计时 var stopwatch = Stopwatch.StartNew(); var auditInfo = new AuditInfo { UserInfo = _Session?.Id, ServiceName = type != null?type.FullName.TruncateWithPostfix(EntityDefault.FieldsLength250) : "", MethodName = method.Name.TruncateWithPostfix(EntityDefault.FieldsLength250), ////请求参数转Json Parameters = JsonConvert.SerializeObject(arguments), ExecutionTime = DateTime.Now, BrowserInfo = context.HttpContext.Request.Headers["User-Agent"].ToString().TruncateWithPostfix(EntityDefault.FieldsLength250), ClientIpAddress = context.HttpContext.Connection.RemoteIpAddress.ToString().TruncateWithPostfix(EntityDefault.FieldsLength50), //ClientName = _clientInfoProvider.ComputerName.TruncateWithPostfix(EntityDefault.FieldsLength100), }; ActionExecutedContext result = null; try { result = await next(); if (result.Exception != null && !result.ExceptionHandled) { auditInfo.Exception = result.Exception.ToString(); } } catch (Exception ex) { auditInfo.Exception = ex.ToString(); throw; } finally { stopwatch.Stop(); auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); if (result != null) { switch (result.Result) { case ObjectResult objectResult: auditInfo.ReturnValue = JsonConvert.SerializeObject(objectResult.Value); break; case JsonResult jsonResult: auditInfo.ReturnValue = JsonConvert.SerializeObject(jsonResult.Value); break; case ContentResult contentResult: auditInfo.ReturnValue = contentResult.Content; break; } } Console.WriteLine(auditInfo.ToString()); var miniPro = MiniProfiler.Current; using (miniPro.Step("Add AuditLog")) { //保存审计日志 await _auditLogService.SaveAsync(auditInfo); } } }