0
0

LogHandler.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Linq;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using Admin.Core.Common.Output;
  5. using Admin.Core.Service.Admin.OprationLog;
  6. using Admin.Core.Service.Admin.OprationLog.Input;
  7. using Microsoft.AspNetCore.Mvc.Filters;
  8. //using Newtonsoft.Json;
  9. namespace Admin.Core.Logs
  10. {
  11. /// <summary>
  12. /// 操作日志处理
  13. /// </summary>
  14. public class LogHandler : ILogHandler
  15. {
  16. private readonly ApiHelper _apiHelper;
  17. private readonly IOprationLogService _oprationLogService;
  18. public LogHandler(
  19. ApiHelper apiHelper,
  20. IOprationLogService oprationLogService
  21. )
  22. {
  23. _apiHelper = apiHelper;
  24. _oprationLogService = oprationLogService;
  25. }
  26. public async Task LogAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  27. {
  28. var sw = new Stopwatch();
  29. sw.Start();
  30. dynamic actionResult = (await next()).Result;
  31. sw.Stop();
  32. //操作参数
  33. //var args = JsonConvert.SerializeObject(context.ActionArguments);
  34. //操作结果
  35. //var result = JsonConvert.SerializeObject(actionResult?.Value);
  36. var res = actionResult?.Value as IResponseOutput;
  37. var input = new OprationLogAddInput
  38. {
  39. ApiMethod = context.HttpContext.Request.Method.ToLower(),
  40. ApiPath = context.ActionDescriptor.AttributeRouteInfo.Template.ToLower(),
  41. ElapsedMilliseconds = sw.ElapsedMilliseconds,
  42. Status = res?.Success,
  43. Msg = res?.Msg
  44. };
  45. input.ApiLabel = _apiHelper.GetApis().FirstOrDefault(a => a.Path == input.ApiPath)?.Label;
  46. await _oprationLogService.AddAsync(input);
  47. }
  48. }
  49. }