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