/// <summary> /// 执行器 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { #region //base.OnActionExecuting(context); // 后续添加了获取请求的请求体,如果在实际项目中不需要删除即可 //long contentLen = context.HttpContext.Request.ContentLength == null ? 0 : context.HttpContext.Request.ContentLength.Value; //if (contentLen > 0) //{ // // 读取请求体中所有内容 // System.IO.Stream stream = context.HttpContext.Request.Body; // if (context.HttpContext.Request.Method == "POST") // { // stream.Position = 0; // } // byte[] buffer = new byte[contentLen]; // stream.Read(buffer, 0, buffer.Length); // // 转化为字符串 // _requestBody = System.Text.Encoding.UTF8.GetString(buffer); //} #endregion //请求Url url = context.HttpContext.Request.Host + context.HttpContext.Request.Path.Value + context.HttpContext.Request.QueryString; //string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString; //string method = context.Request.Method; controller = context.RouteData.Values["controller"].ToString(); action = context.RouteData.Values["action"].ToString(); parameters = Newtonsoft.Json.JsonConvert.SerializeObject(context.ActionArguments); ipaddress = context.HttpContext.Connection.RemoteIpAddress.ToString(); clientname = ""; browserinfo = context.HttpContext.Request.Headers["User-Agent"]; claimsPrincipal = context.HttpContext.User; var userid = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; //_stopwatch = new Stopwatch(); //_stopwatch.Start(); // 开始性能计数 var stopwatch = Stopwatch.StartNew(); try { // 尝试调用接口方法 var result = await next(); // 产生异常之后,将其异常信息存放在审计信息之中 if (result.Exception != null && !result.ExceptionHandled) { exception = result.Exception.Message; } } catch (Exception e) { // 产生异常之后,将其异常信息存放在审计信息之中 exception = e.Message; throw; } finally { // 停止计数,并且存储审计信息 stopwatch.Stop(); AuditLog entitylog = new AuditLog(); entitylog.SystemCode = "Stat001"; entitylog.TenantId = 1; entitylog.UserId = string.IsNullOrEmpty(userid) == false?Convert.ToInt64(userid) : 0; entitylog.ServiceName = entitylog.ClipString(controller, AuditLog.MaxServiceNameLength); entitylog.MethodName = entitylog.ClipString(action, AuditLog.MaxMethodNameLength); entitylog.Parameters = entitylog.ClipString(parameters, AuditLog.MaxParametersLength); entitylog.ExecutionTime = System.DateTime.Now; entitylog.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); entitylog.ClientIpAddress = entitylog.ClipString(ipaddress, AuditLog.MaxClientIpAddressLength); entitylog.ClientName = entitylog.ClipString(clientname, AuditLog.MaxClientNameLength); entitylog.BrowserInfo = entitylog.ClipString(browserinfo, AuditLog.MaxBrowserInfoLength); entitylog.Exception = exception == null ? null : entitylog.ClipString(exception, AuditLog.MaxExceptionLength); entitylog.ImpersonatorUserId = null; entitylog.ImpersonatorTenantId = 1; entitylog.CustomData = entitylog.ClipString(url, AuditLog.MaxCustomDataLength); await _auditLogRepository.InsertAsync(entitylog); } }
public async Task InvokeAsync(HttpContext context) { _startTime = DateTime.Now; try { //请求Url url = context.Request.Host + context.Request.Path.Value + context.Request.QueryString; //string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString; //string method = context.Request.Method; controller = ""; action = ""; parameters = ""; ipaddress = context.Request.HttpContext.Connection.RemoteIpAddress.ToString(); clientname = ""; browserinfo = context.Request.Headers["User-Agent"]; claimsPrincipal = context.User; var userid = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; //var requestMessage = context.Request.Form["RequestMessage"]; // 后续添加了获取请求的请求体,如果在实际项目中不需要删除即可 long contentLen = context.Request.ContentLength == null ? 0 : context.Request.ContentLength.Value; //if (contentLen > 0) //{ // // 读取请求体中所有内容 // System.IO.Stream stream = context.Request.Body; // if (context.Request.Method == "POST") // { // stream.Position = 0; // } // byte[] buffer = new byte[contentLen]; // stream.Read(buffer, 0, buffer.Length); // // 转化为字符串 // _requestBody = System.Text.Encoding.UTF8.GetString(buffer); //} parameters = Newtonsoft.Json.JsonConvert.SerializeObject(context.Request.HttpContext); await _next(context); // } catch (Exception e) { exception = e.Message; } finally { AuditLog entitylog = new AuditLog(); entitylog.SystemCode = "Stat001"; entitylog.TenantId = 1; entitylog.UserId = string.IsNullOrEmpty(userid) == false?Convert.ToInt64(userid) : 0; entitylog.ServiceName = entitylog.ClipString(controller, AuditLog.MaxServiceNameLength); entitylog.MethodName = entitylog.ClipString(action, AuditLog.MaxMethodNameLength); entitylog.Parameters = entitylog.ClipString(parameters, AuditLog.MaxParametersLength); entitylog.ExecutionTime = System.DateTime.Now; entitylog.ExecutionDuration = Convert.ToInt32((DateTime.Now - _startTime).TotalMilliseconds); entitylog.ClientIpAddress = entitylog.ClipString(ipaddress, AuditLog.MaxClientIpAddressLength); entitylog.ClientName = entitylog.ClipString(clientname, AuditLog.MaxClientNameLength); entitylog.BrowserInfo = entitylog.ClipString(browserinfo, AuditLog.MaxBrowserInfoLength); entitylog.Exception = entitylog.ClipString(exception, AuditLog.MaxExceptionLength); entitylog.ImpersonatorUserId = null; entitylog.ImpersonatorTenantId = 1; entitylog.CustomData = entitylog.ClipString(url, AuditLog.MaxCustomDataLength); await _auditLogRepository.InsertAsync(entitylog); } await _next(context); }