LogController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using DW5S.DTO;
  8. using DW5S.Repostory;
  9. using System.IO;
  10. using DW5S.Entity;
  11. using DW5S.WebApi;
  12. using System.Diagnostics;
  13. using System.Configuration;
  14. using Microsoft.AspNetCore.Mvc;
  15. using Microsoft.EntityFrameworkCore;
  16. using Serilog;
  17. namespace DW5S.Controllers
  18. {
  19. /// <summary>
  20. /// 日志相关接口
  21. /// </summary>
  22. public class LogController : BaseController
  23. {
  24. ILogger logger { get; set; }
  25. IUnitOfWork unitOfWork { get; set; }
  26. /// <summary>
  27. /// 将日志写入到平台
  28. /// </summary>
  29. /// <param name="dto">日志信息</param>
  30. /// <returns></returns>
  31. [HttpPost]
  32. public async Task<AjaxResult> Add(LogInfoDto dto)
  33. {
  34. try
  35. {
  36. var repsLog = unitOfWork.OfLong<LogRes>();
  37. await repsLog.AddOrUpdateAsync(new LogRes()
  38. {
  39. LogTime = dto.LogTime,
  40. Module = dto.Module,
  41. LogType = (Entity.EnumLogType)(int)dto.LogType,
  42. Msg = dto.Msg,
  43. });
  44. await unitOfWork.SaveAsync();
  45. return Success();
  46. }
  47. catch (Exception ex)
  48. {
  49. string msg = "日志信息写入异常";
  50. logger.Error(ex, msg);
  51. return Error(msg);
  52. }
  53. }
  54. /// <summary>
  55. /// 查询日志信息
  56. /// </summary>
  57. /// <param name="dto">日志查询参数</param>
  58. /// <returns></returns>
  59. [HttpPost]
  60. public async Task<AjaxResult<List<LogInfoDto>>> Query(LogQueryDto dto)
  61. {
  62. try
  63. {
  64. var query = unitOfWork.OfLong<LogRes>().AsQueryable();
  65. if (!string.IsNullOrWhiteSpace(dto.Module) && dto.Module != "全部")
  66. query = query.Where(p => p.Module == dto.Module);
  67. if (dto.LogTimeBegin != null && dto.LogTimeBegin != DateTime.MinValue)
  68. query = query.Where(p => p.LogTime >= dto.LogTimeBegin);
  69. if (dto.LogTimeEnd != null && dto.LogTimeEnd != DateTime.MinValue)
  70. query = query.Where(p => p.LogTime <= dto.LogTimeEnd);
  71. var res = await query.OrderBy(p => p.Module).ThenByDescending(t => t.LogTime).ToListAsync();
  72. var dtoRes = res.Select(t => new LogInfoDto
  73. {
  74. ID = t.Id,
  75. LogTime = t.LogTime,
  76. LogType = (EnumLogTypeDto)(int)t.LogType,
  77. Module = t.Module,
  78. Msg = t.Msg,
  79. }).ToList();
  80. return Success(dtoRes);
  81. }
  82. catch (Exception ex)
  83. {
  84. string msg = "日志信息查询异常";
  85. logger.Error(ex, msg);
  86. return Error<List<LogInfoDto>>(msg);
  87. }
  88. }
  89. /// <summary>
  90. /// 获取所有日志模块
  91. /// </summary>
  92. /// <returns></returns>
  93. [HttpPost]
  94. public async Task<AjaxResult<List<LogModulesResDto>>> GetModules()
  95. {
  96. try
  97. {
  98. var repsLog = unitOfWork.OfLong<LogRes>();
  99. var res = await repsLog.GetDistinctAsync(p => p.Module);
  100. var dtoRes = res.Select(p => new LogModulesResDto()
  101. {
  102. Module = p
  103. }).OrderBy(p => p.Module).ToList();
  104. return Success(dtoRes);
  105. }
  106. catch (Exception ex)
  107. {
  108. string msg = "日志模块查询异常";
  109. logger.Error(ex, msg);
  110. return Error<List<LogModulesResDto>>(msg);
  111. }
  112. }
  113. /// <summary>
  114. /// 删除日志
  115. /// </summary>
  116. /// <param name="dto"></param>
  117. /// <returns></returns>
  118. [HttpPost]
  119. public async Task<AjaxResult> Delete(IEnumerable<LogDeleteDto> dto)
  120. {
  121. if (dto == null || !dto.Any()) return Success();
  122. try
  123. {
  124. var repsLog = unitOfWork.OfLong<LogRes>();
  125. await repsLog.DeleteAsync(dto.To<List<LogRes>>());
  126. await unitOfWork.SaveAsync();
  127. return Success();
  128. }
  129. catch (Exception ex)
  130. {
  131. string msg = "日志信息删除异常";
  132. logger.Error(ex, msg);
  133. return Error(msg);
  134. }
  135. }
  136. }
  137. }