SysLogService.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using YBEE.EQM.Core;
  2. namespace YBEE.EQM.Application
  3. {
  4. /// <summary>
  5. /// 日志管理服务
  6. /// </summary>
  7. public class SysLogService : ISysLogService, ITransient
  8. {
  9. private readonly IRepository _rep; // 操作日志表仓储
  10. public SysLogService(IRepository rep)
  11. {
  12. _rep = rep;
  13. }
  14. #region 异常日志
  15. /// <summary>
  16. /// 分页查询异常日志
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <returns></returns>
  20. //[HttpGet("sysExLog/page")]
  21. public async Task<PageResult<SysLogExOutput>> QueryLogExPageList(SysLogExPageInput input)
  22. {
  23. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  24. var className = !string.IsNullOrEmpty(input.ClassName?.Trim());
  25. var methodName = !string.IsNullOrEmpty(input.MethodName?.Trim());
  26. var exceptionMsg = !string.IsNullOrEmpty(input.ExceptionMsg?.Trim());
  27. var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
  28. var exLogs = await _rep.Change<SysLogEx>().DetachedEntities
  29. .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
  30. .Where(className, u => u.ClassName == input.ClassName)
  31. .Where(methodName, u => u.MethodName == input.MethodName)
  32. .Where(exceptionMsg, u => EF.Functions.Like(u.ExceptionMsg, $"%{input.ExceptionMsg.Trim()}%"))
  33. .Where(searchBeginTime, u => u.ExceptionTime >= DateTime.Parse(input.SearchBeginTime.Trim()) &&
  34. u.ExceptionTime <= DateTime.Parse(input.SearchEndTime.Trim()))
  35. .OrderByDescending(u => u.Id)
  36. .ProjectToType<SysLogExOutput>()
  37. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  38. return exLogs;
  39. }
  40. /// <summary>
  41. /// 清空异常日志
  42. /// </summary>
  43. /// <returns></returns>
  44. public async Task ClearLogEx()
  45. {
  46. var rep = _rep.Change<SysLogEx>();
  47. await rep.SqlNonQueryAsync("TRUNCATE TABLE sys_log_ex");
  48. await rep.SaveNowAsync();
  49. }
  50. #endregion
  51. #region 操作日志
  52. /// <summary>
  53. /// 分页查询操作日志
  54. /// </summary>
  55. /// <param name="input"></param>
  56. /// <returns></returns>
  57. public async Task<PageResult<SysLogOpOutput>> QueryLogOpPageList(SysLogOpPageInput input)
  58. {
  59. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  60. var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
  61. var searchEndTime = !string.IsNullOrEmpty(input.SearchEndTime?.Trim());
  62. var ReqMethod = !string.IsNullOrEmpty(input.ReqMethod?.Trim());
  63. var opLogs = await _rep.Change<SysLogOp>().DetachedEntities
  64. .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
  65. .Where(input.Success.HasValue, u => u.Success == input.Success)
  66. .Where(ReqMethod, u => u.ReqMethod == input.ReqMethod)
  67. .Where(searchBeginTime, u => u.OpTime >= DateTime.Parse(input.SearchBeginTime.Trim()))
  68. .Where(searchEndTime, u => u.OpTime <= DateTime.Parse(input.SearchEndTime.Trim()))
  69. .OrderByDescending(u => u.Id)
  70. .ProjectToType<SysLogOpOutput>()
  71. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  72. return opLogs;
  73. }
  74. /// <summary>
  75. /// 清空操作日志
  76. /// </summary>
  77. /// <returns></returns>
  78. public async Task ClearLogOp()
  79. {
  80. var rep = _rep.Change<SysLogOp>();
  81. await rep.SqlNonQueryAsync("TRUNCATE TABLE sys_log_op");
  82. await rep.SaveNowAsync();
  83. }
  84. #endregion
  85. #region 访问日志
  86. /// <summary>
  87. /// 分页查询访问日志
  88. /// </summary>
  89. /// <param name="input"></param>
  90. /// <returns></returns>
  91. public async Task<PageResult<SysLogVisOutput>> QueryLogVisPageList(SysLogVisPageInput input)
  92. {
  93. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  94. var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
  95. var visLogs = await _rep.Change<SysLogVis>().DetachedEntities
  96. .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
  97. .Where(input.VisType >= 0, u => u.VisType == input.VisType)
  98. .Where(input.Success.HasValue, u => u.Success == input.Success)
  99. .Where(searchBeginTime, u => u.VisTime >= DateTime.Parse(input.SearchBeginTime.Trim()) &&
  100. u.VisTime <= DateTime.Parse(input.SearchEndTime.Trim()))
  101. .OrderByDescending(u => u.Id)
  102. .ProjectToType<SysLogVisOutput>()
  103. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  104. return visLogs;
  105. }
  106. /// <summary>
  107. /// 清空访问日志
  108. /// </summary>
  109. /// <returns></returns>
  110. public async Task ClearLogVis()
  111. {
  112. var rep = _rep.Change<SysLogVis>();
  113. await rep.SqlNonQueryAsync("TRUNCATE TABLE sys_log_vis");
  114. await rep.SaveNowAsync();
  115. }
  116. #endregion
  117. }
  118. }