BaseController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Filters;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using YiSha.Entity.SystemManage;
  10. using YiSha.Enum;
  11. using YiSha.IBusiness.SystemManage;
  12. using YiSha.Util;
  13. using YiSha.Util.Extension;
  14. using YiSha.Util.Model;
  15. using YiSha.Web.Code;
  16. namespace YiSha.Admin.Web.Controllers
  17. {
  18. /// <summary>
  19. /// 基础控制器,用来记录访问日志
  20. /// </summary>
  21. public class BaseController : Controller
  22. {
  23. public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
  24. {
  25. ILogOperateBLL logOperateBLL = (ILogOperateBLL)context.HttpContext.RequestServices.GetService(typeof(ILogOperateBLL));
  26. Stopwatch sw = new Stopwatch();
  27. sw.Start();
  28. string action = context.RouteData.Values["Action"].ParseToString();
  29. OperatorInfo user = await Operator.Instance.Current();
  30. if (GlobalContext.SystemConfig.Demo)
  31. {
  32. if (context.HttpContext.Request.Method.ToUpper() == "POST")
  33. {
  34. string[] allowAction = new string[] { "LoginJson", "ExportUserJson", "CodePreviewJson" };
  35. if (!allowAction.Select(p => p.ToUpper()).Contains(action.ToUpper()))
  36. {
  37. TData obj = new TData();
  38. obj.Message = "演示模式,不允许操作";
  39. context.Result = new JsonResult(obj);
  40. return;
  41. }
  42. }
  43. }
  44. var resultContext = await next();
  45. sw.Stop();
  46. string ip = NetHelper.Ip;
  47. LogOperateEntity operateEntity = new LogOperateEntity();
  48. var areaName = context.RouteData.DataTokens["area"] + "/";
  49. var controllerName = context.RouteData.Values["controller"] + "/";
  50. string currentUrl = "/" + areaName + controllerName + action;
  51. string[] notLogAction = new string[] { "GetServerJson", "Error" };
  52. if (!notLogAction.Select(p => p.ToUpper()).Contains(action.ToUpper()))
  53. {
  54. #region 获取请求参数
  55. switch (context.HttpContext.Request.Method.ToUpper())
  56. {
  57. case "GET":
  58. operateEntity.ExecuteParam = context.HttpContext.Request.QueryString.Value.ParseToString();
  59. break;
  60. case "POST":
  61. if (context.ActionArguments?.Count > 0)
  62. {
  63. operateEntity.ExecuteUrl += context.HttpContext.Request.QueryString.Value.ParseToString();
  64. operateEntity.ExecuteParam = TextHelper.GetSubString(JsonConvert.SerializeObject(context.ActionArguments), 4000);
  65. }
  66. else
  67. {
  68. operateEntity.ExecuteParam = context.HttpContext.Request.QueryString.Value.ParseToString();
  69. }
  70. break;
  71. }
  72. #endregion
  73. #region 异常获取
  74. StringBuilder sbException = new StringBuilder();
  75. if (resultContext.Exception != null)
  76. {
  77. Exception exception = resultContext.Exception;
  78. sbException.AppendLine(exception.Message);
  79. while (exception.InnerException != null)
  80. {
  81. sbException.AppendLine(exception.InnerException.Message);
  82. exception = exception.InnerException;
  83. }
  84. sbException.AppendLine(resultContext.Exception.StackTrace);
  85. operateEntity.LogStatus = OperateStatusEnum.Fail.ParseToInt();
  86. }
  87. else
  88. {
  89. operateEntity.LogStatus = OperateStatusEnum.Success.ParseToInt();
  90. }
  91. #endregion
  92. #region 日志实体
  93. if (user != null)
  94. {
  95. operateEntity.BaseCreatorId = user.UserId;
  96. }
  97. operateEntity.ExecuteTime = sw.ElapsedMilliseconds.ParseToInt();
  98. operateEntity.IpAddress = ip;
  99. operateEntity.ExecuteUrl = currentUrl.Replace("//", "/");
  100. operateEntity.ExecuteResult = TextHelper.GetSubString(sbException.ToString(), 4000);
  101. #endregion
  102. Action taskAction = async () =>
  103. {
  104. // 让底层不用获取HttpContext
  105. operateEntity.BaseCreatorId = operateEntity.BaseCreatorId;
  106. // 耗时的任务异步完成
  107. // operateEntity.IpLocation = IpLocationHelper.GetIpLocation(ip);
  108. await logOperateBLL.SaveForm(operateEntity);
  109. };
  110. AsyncTaskHelper.StartTask(taskAction);
  111. }
  112. }
  113. public override void OnActionExecuted(ActionExecutedContext context)
  114. {
  115. base.OnActionExecuted(context);
  116. }
  117. }
  118. }