LogController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web.Http;
  9. using Serilog;
  10. using XdCxRhDW.Dto;
  11. using XdCxRhDW.Repostory;
  12. using System.IO;
  13. using XdCxRhDW.Entity;
  14. using XdCxRhDW.Api;
  15. using XdCxRhDW.WebApi;
  16. using DPP_YH_Core.Extensions;
  17. using XdCxRhDW.App.Model;
  18. using System.Diagnostics;
  19. using System.Configuration;
  20. namespace XdCxRhDW.App.Controllers
  21. {
  22. /// <summary>
  23. /// 日志相关接口
  24. /// </summary>
  25. public class LogController : BaseController
  26. {
  27. /// <summary>
  28. /// 将日志写入到平台
  29. /// </summary>
  30. /// <param name="dto">日志信息</param>
  31. /// <returns></returns>
  32. [HttpPost]
  33. public async Task<AjaxResult> Add(LogInfoDto dto)
  34. {
  35. try
  36. {
  37. using (RHDWContext db = new RHDWContext())
  38. {
  39. LogRes res = new LogRes()
  40. {
  41. LogTime = dto.LogTime,
  42. Module = dto.Module,
  43. LogType = (Entity.EnumLogType)(int)dto.LogType,
  44. Msg = dto.Msg,
  45. };
  46. db.LogRes.Add(res);
  47. await db.SaveChangesAsync();
  48. }
  49. return Success();
  50. }
  51. catch (TaskCanceledException)
  52. {
  53. Serilog.Log.Warning("日志信息写入超时");
  54. return Error("日志信息写入超时");
  55. }
  56. catch (Exception ex)
  57. {
  58. Serilog.Log.Error(ex, "日志信息写入异常");
  59. return Error("日志信息写入异常");
  60. }
  61. }
  62. /// <summary>
  63. /// 查询日志信息
  64. /// </summary>
  65. /// <param name="dto">日志查询参数</param>
  66. /// <returns></returns>
  67. [HttpPost]
  68. public async Task<AjaxResult<List<LogInfoDto>>> Query(LogQueryDto dto)
  69. {
  70. try
  71. {
  72. using (RHDWContext db = new RHDWContext())
  73. {
  74. var query = db.LogRes.AsQueryable();
  75. if (!string.IsNullOrWhiteSpace(dto.Module) && dto.Module != "全部")
  76. query = query.Where(p => p.Module == dto.Module);
  77. if (dto.LogTimeBegin != null && dto.LogTimeBegin != DateTime.MinValue)
  78. query = query.Where(p => p.LogTime >= dto.LogTimeBegin);
  79. if (dto.LogTimeEnd != null && dto.LogTimeEnd != DateTime.MinValue)
  80. query = query.Where(p => p.LogTime <= dto.LogTimeEnd);
  81. var res = await query.OrderBy(p => p.Module).ThenByDescending(t => t.LogTime).ToListAsync();
  82. var dtoRes = res.Select(t => new LogInfoDto
  83. {
  84. ID = t.ID,
  85. LogTime = t.LogTime,
  86. LogType = (EnumLogTypeDto)(int)t.LogType,
  87. Module = t.Module,
  88. Msg = t.Msg,
  89. }).ToList();
  90. return Success(dtoRes);
  91. }
  92. }
  93. catch (TaskCanceledException)
  94. {
  95. Serilog.Log.Warning($"日志信息查询超时.Mudule={dto.Module},TimeStart={dto.LogTimeBegin},TimeEnd={dto.LogTimeEnd}");
  96. return Error<List<LogInfoDto>>("日志信息查询超时");
  97. }
  98. catch (Exception ex)
  99. {
  100. Serilog.Log.Error(ex, "日志信息查询异常.Mudule={dto.Module},TimeStart={dto.LogTimeBegin},TimeEnd={dto.LogTimeEnd}");
  101. return Error<List<LogInfoDto>>("日志信息查询异常");
  102. }
  103. }
  104. /// <summary>
  105. /// 获取所有日志模块
  106. /// </summary>
  107. /// <returns></returns>
  108. [HttpPost]
  109. public async Task<AjaxResult<List<LogModulesResDto>>> GetModules()
  110. {
  111. try
  112. {
  113. using (RHDWContext db = new RHDWContext())
  114. {
  115. var res = await db.LogRes.Select(p => p.Module).Distinct().ToListAsync();
  116. var dtoRes = res.Select(p => new LogModulesResDto()
  117. {
  118. Module = p
  119. }).OrderBy(p => p.Module).ToList();
  120. return Success(dtoRes);
  121. }
  122. }
  123. catch (TaskCanceledException)
  124. {
  125. Serilog.Log.Warning($"日志模块查询超时");
  126. return Error<List<LogModulesResDto>>("日志模块查询超时");
  127. }
  128. catch (Exception ex)
  129. {
  130. Serilog.Log.Error(ex, "日志模块查询异常");
  131. return Error<List<LogModulesResDto>>("日志模块查询异常");
  132. }
  133. }
  134. /// <summary>
  135. /// 删除日志
  136. /// </summary>
  137. /// <param name="dto"></param>
  138. /// <returns></returns>
  139. [HttpPost]
  140. public async Task<AjaxResult> Delete(IEnumerable<LogDeleteDto> dto)
  141. {
  142. if (dto == null || !dto.Any()) return Success();
  143. try
  144. {
  145. var ids = dto.Select(p => p.ID);
  146. using (RHDWContext db = new RHDWContext())
  147. {
  148. var delItems = await db.LogRes.Where(p => ids.Contains(p.ID)).ToListAsync();
  149. var delItemsReal = db.LogRes.RemoveRange(delItems);
  150. db.SaveChanges();
  151. await Add(new LogInfoDto()
  152. {
  153. LogTime = DateTime.Now,
  154. LogType = EnumLogTypeDto.Info,
  155. Module = "平台",
  156. Msg = $"用户删除了{delItemsReal.LongCount()}行日志"
  157. });
  158. return Success();
  159. }
  160. }
  161. catch (TaskCanceledException)
  162. {
  163. Serilog.Log.Warning($"日志信息删除超时.共{dto.Count()}条记录");
  164. return Error("日志信息删除超时");
  165. }
  166. catch (Exception ex)
  167. {
  168. Serilog.Log.Error(ex, "日志信息删除异常.共{dto.Count()}条记录");
  169. return Error("日志信息删除异常");
  170. }
  171. }
  172. }
  173. }