LogFilter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using DopInterfacePlatform.Entity;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.Logging;
  6. using System.Diagnostics;
  7. namespace DopInterfacePlatform
  8. {
  9. /// <summary>
  10. /// 记录日志
  11. /// </summary>
  12. public class LogFilter : ActionFilterAttribute
  13. {
  14. // private IBusApiOperatorRepository api_operator_repository;
  15. private Stopwatch stopwach { get; set; }
  16. private string actionArguments { get; set; }
  17. private Dictionary<string, object> keyValuePairs { set; get; }
  18. private readonly DopInterfacePlatformContext _context;
  19. public LogFilter(DopInterfacePlatformContext context)
  20. {
  21. _context = context;
  22. }
  23. public override void OnActionExecuting(ActionExecutingContext context)
  24. {
  25. base.OnActionExecuting(context);
  26. //is not controller return
  27. if (!(context.ActionDescriptor is ControllerActionDescriptor action))
  28. return;
  29. var request = context.HttpContext.Request;
  30. //启动倒带方式
  31. request.EnableBuffering();
  32. if (request.Method.ToLower().Equals("post"))
  33. {
  34. request.Body.Position = 0;
  35. using var requestReader = new StreamReader(request.Body);
  36. var requestContent = requestReader.ReadToEnd();
  37. request.Body.Position = 0;
  38. }
  39. stopwach = new Stopwatch();
  40. stopwach.Start();
  41. if(context.ActionArguments.Count>0)
  42. {
  43. actionArguments = context.ActionArguments.ToJsonString();
  44. }
  45. stopwach = new Stopwatch();
  46. stopwach.Start();
  47. }
  48. public override void OnActionExecuted(ActionExecutedContext context)
  49. {
  50. base.OnActionExecuted(context);
  51. //is not controller return
  52. if (!(context.ActionDescriptor is ControllerActionDescriptor action))
  53. return;
  54. stopwach.Stop();
  55. var time = stopwach.Elapsed;
  56. // string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
  57. string logMethod = context.HttpContext.Request.Method;
  58. dynamic result = context.Result.GetType().Name == "EmptyResult"
  59. ? new { Value = "无返回结果" }
  60. : context.Result as dynamic;
  61. string res = "在返回结果前发生了异常";
  62. try
  63. {
  64. if (result != null)
  65. {
  66. res = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value);
  67. }
  68. }
  69. catch (System.Exception ex)
  70. {
  71. res = "日志未获取到结果,返回的数据无法序列化!" + ex.Message;
  72. }
  73. //转换ActionDescriptor
  74. string logController = context.RouteData.Values["Controller"].ToString();
  75. var logAction = context.RouteData.Values["Action"].ToString();
  76. var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
  77. var ipAddress = context.HttpContext.Connection.RemoteIpAddress.ToString();
  78. if(logAction!="GetToken")
  79. {
  80. var model = new InterfacePlatformLog();
  81. if (actionArguments == null)
  82. {
  83. actionArguments = "";
  84. }
  85. model.operatorcol_time = DateTime.Now;
  86. model.operatorcol_ip = ipAddress;
  87. model.operatorcol_duration = (int)time.TotalSeconds * 1000;
  88. model.operatorcol_request = $"{logController}-{logAction}-{logMethod}-{actionArguments}"; //调用入参
  89. model.operatorcol_respone = res; //调用出参
  90. _context.InterfacePlatformLog.Add(model);
  91. _context.SaveChanges();
  92. }
  93. }
  94. }
  95. }