public async Task LogAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var sw = new Stopwatch(); sw.Start(); dynamic actionResult = (await next()).Result; sw.Stop(); //操作参数 //var args = JsonConvert.SerializeObject(context.ActionArguments); //操作结果 //var result = JsonConvert.SerializeObject(actionResult?.Value); var res = actionResult?.Value as IResponseOutput; var input = new OprationLogAddInput { ApiMethod = context.HttpContext.Request.Method.ToLower(), ApiPath = context.ActionDescriptor.AttributeRouteInfo.Template.ToLower(), ElapsedMilliseconds = sw.ElapsedMilliseconds, Status = res?.Success, Msg = res?.Msg }; input.ApiLabel = _apiHelper.GetApis().FirstOrDefault(a => a.Path == input.ApiPath)?.Label; await _oprationLogService.AddAsync(input); }
//public async Task<IResultModel> PageAsync(PageInput<OprationLogEntity> input) //{ // var userName = input.Filter?.CreatedUserName; // var list = await _oprationLogRepository.Select // .WhereIf(userName.NotNull(), a => a.CreatedUserName.Contains(userName)) // .Count(out var total) // .OrderByDescending(true, c => c.Id) // .Page(input.CurrentPage, input.PageSize) // .ToListAsync<OprationLogListOutput>(); // var data = new PageOutput<OprationLogListOutput>() // { // List = list, // Total = total // }; // return ResponseOutput.Ok(data); //} public async Task <IResultModel> AddAsync(OprationLogAddInput input) { string ua = _context.HttpContext.Request.Headers["User-Agent"]; var client = UAParser.Parser.GetDefault().Parse(ua); var device = client.Device.Family; device = device.ToLower() == "other" ? "" : device; input.Browser = client.UA.Family; input.Os = client.OS.Family; input.Device = device; input.BrowserInfo = ua; input.NickName = _user.NickName; input.IP = IPHelper.GetIP(_context?.HttpContext?.Request); var entity = _mapper.Map <OprationLogEntity>(input); var id = await _oprationLogRepository.InsertAsync(entity); return(ResultModel.Result(id > 0)); }
public async Task LogAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var sw = new Stopwatch(); sw.Start(); var actionExecutedContext = await next(); sw.Stop(); //操作参数 //var args = JsonConvert.SerializeObject(context.ActionArguments); //操作结果 //var result = JsonConvert.SerializeObject(actionResult?.Value); try { var input = new OprationLogAddInput { ApiMethod = context.HttpContext.Request.Method.ToLower(), ApiPath = context.ActionDescriptor.AttributeRouteInfo.Template.ToLower(), ElapsedMilliseconds = sw.ElapsedMilliseconds }; if (actionExecutedContext.Result is ObjectResult result && result.Value is IResponseOutput res) { input.Status = res.Success; input.Msg = res.Msg; } input.ApiLabel = _apiHelper.GetApis().FirstOrDefault(a => a.Path == input.ApiPath)?.Label; await _oprationLogService.AddAsync(input); } catch (Exception ex) { _logger.LogError("操作日志插入异常:{@ex}", ex); } }