SysLogService.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using WMS.BZModels.Dto.UserCenterManager.UserDeptDtos;
  10. using WMS.BZModels;
  11. using WMS.BZModels.Models.UserCenterManager;
  12. using WMS.Info;
  13. using WMS.BZSqlSugar;
  14. using WMS.Util;
  15. using WMS.BZModels.Dto.UserCenterManager.SysLogDtos;
  16. namespace WMS.BZServices.UserCenterManager
  17. {
  18. public class SysLogService
  19. {
  20. private static readonly Repository<SysLog> _sysLogRepository=new Repository<SysLog> ();
  21. //public SysLogService(Repository<SysLog> sysLogRepository)
  22. //{
  23. // _sysLogRepository = sysLogRepository;
  24. //}
  25. public static void WriteLog(BZBLLExecInfo BLLExec, ELogType eLogType)
  26. {
  27. try
  28. {
  29. SysLog log = new SysLog();
  30. log.RunMs = (int)CommonUtil.TimerEnd(BLLExec.RunWatch);
  31. log.LogTime = DateTime.Now;
  32. log.AppTypeNum = 0;
  33. log.TypeNum = (int)eLogType;
  34. if (BLLExec.Ex == null)
  35. {
  36. log.ContentText = string.IsNullOrWhiteSpace(BLLExec.BLLObj.SuccessMsg) ? "操作成功。" : BLLExec.BLLObj.SuccessMsg;
  37. log.IsSucc = 1;
  38. }
  39. else
  40. {
  41. log.ContentText = BLLExec.Ex.Message;
  42. log.IsSucc = 0;
  43. }
  44. if (BLLExec.BLLObj != null)
  45. {
  46. log.AppTypeNum = (int)BLLExec.BLLObj.LoginUser.AppType;
  47. log.Method = BZBLLCore.GetBLLClassNo(BLLExec.BLLObj);
  48. log.MethodText = BLLExec.BLLObj.BLLDesc;
  49. StringBuilder sb = new StringBuilder();
  50. sb.Append("---------------------------------------请求数据-----------------------------------------------------------------");
  51. sb.AppendLine(BLLExec.BLLObj.InJsonData);
  52. if (BLLExec.Ex != null)
  53. {
  54. sb.AppendLine("----------------------------------------异常数据----------------------------------------------------------------");
  55. sb.AppendLine(BLLExec.Ex.ToString());
  56. sb.AppendLine("----------------------------------------异常内部数据----------------------------------------------------------------");
  57. if (BLLExec.Ex.InnerException == null)
  58. {
  59. sb.AppendLine(string.Format("Message:{0} Source:{1} StackTrace:{2} ", BLLExec.Ex.Message, BLLExec.Ex.Source, BLLExec.Ex.StackTrace));
  60. }
  61. else
  62. {
  63. sb.AppendLine(string.Format("Message:{0} Source:{1} StackTrace:{2} ", BLLExec.Ex.InnerException.Message, BLLExec.Ex.InnerException.Source, BLLExec.Ex.InnerException.StackTrace));
  64. }
  65. }
  66. log.ParamJson = sb.ToString();
  67. log.Ip = BLLExec.BLLObj.LoginUser.IPAddress;
  68. log.LogWho = BLLExec.BLLObj.LoginUser.UserNo;
  69. //log. = BLLExec.BLLObj.LoginUser.AppDeviceNo;
  70. }
  71. _sysLogRepository.Insert(log);
  72. }
  73. catch (Exception ex)
  74. {
  75. LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(ex.ToString());
  76. }
  77. }
  78. public static void WriteLog(LoginUserInfo LoginUser, string ModuleNo, string ModuleName, string UrlAddress)
  79. {
  80. try
  81. {
  82. SysLog log = new SysLog();
  83. log.RunMs = 0;
  84. log.LogTime = DateTime.Now;
  85. log.AppTypeNum = (int)LoginUser.AppType;
  86. log.TypeNum = (int)ELogType.Visit;
  87. log.ContentText = string.Format("{0}[{1},{2}]", ModuleNo, ModuleName, UrlAddress);
  88. log.IsSucc = 1;
  89. log.Ip = LoginUser.IPAddress;
  90. log.Method = "VisitModule";
  91. log.MethodText = "模块访问";
  92. //log.F_APPDEVICENO = LoginUser.AppDeviceNo;
  93. log.LogWho = LoginUser.UserNo;
  94. var d = new
  95. {
  96. ModuleNo,
  97. ModuleName,
  98. UrlAddress,
  99. };
  100. log.ParamJson = d.ToJson();
  101. _sysLogRepository.Insert(log);
  102. }
  103. catch (Exception ex)
  104. {
  105. LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(ex.ToString());
  106. }
  107. }
  108. public static void WriteLog(LoginUserInfo LoginUser, dynamic JsonData, string ClassNo, string ClassDesc, Stopwatch ts, ELogType eLogType, Exception ex)
  109. {
  110. try
  111. {
  112. string injson = JsonData == null ? "" : JsonData.ToJson();
  113. SysLog log = new SysLog();
  114. log.RunMs = (int)CommonUtil.TimerEnd(ts);
  115. log.LogTime = DateTime.Now;
  116. log.AppTypeNum = (int)LoginUser.AppType;
  117. log.TypeNum = (int)eLogType;
  118. log.ContentText = ex == null ? "操作成功。" : ex.Message;
  119. log.IsSucc = ex == null ? 1 : 0;
  120. log.Ip = LoginUser.IPAddress;
  121. log.Method = ClassNo;
  122. log.MethodText = ClassDesc;
  123. //log.F_APPDEVICENO = LoginUser.AppDeviceNo;
  124. log.LogWho = LoginUser.UserNo;
  125. log.ParamJson = injson;
  126. _sysLogRepository.Insert(log);
  127. }
  128. catch (Exception e)
  129. {
  130. CommonUtil.TimerEnd(ts);
  131. LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(e.ToString());
  132. }
  133. }
  134. public static void WriteLog(LoginUserInfo LoginUser, string ClassDesc, dynamic JsonData, Action action)
  135. {
  136. Stopwatch ts = CommonUtil.TimerStart();
  137. StackTrace ss = new StackTrace(true);
  138. //index:0为本身的方法;1为调用方法;2为其上上层,依次类推
  139. MethodBase mb = ss.GetFrame(1).GetMethod();
  140. string ClassNo = mb.ReflectedType.FullName + "." + mb.Name;
  141. try
  142. {
  143. action.Invoke();
  144. WriteLog(LoginUser, JsonData, ClassNo, ClassDesc, ts, ELogType.Work, null);
  145. }
  146. catch (Exception ex)
  147. {
  148. WriteLog(LoginUser, JsonData, ClassNo, ClassDesc, ts, ELogType.Work, ex);
  149. throw ex;
  150. }
  151. }
  152. /// <summary>
  153. /// 清空日志
  154. /// </summary>
  155. /// <param name="categoryId">日志分类Id</param>
  156. /// <param name="keepTime">保留时间段内</param>
  157. public static void RemoveLog(int LogType, string keepTime)
  158. {
  159. try
  160. {
  161. DateTime operateTime = DateTime.Now;
  162. if (keepTime == "7")//保留近一周
  163. {
  164. operateTime = DateTime.Now.AddDays(-7);
  165. }
  166. else if (keepTime == "1")//保留近一个月
  167. {
  168. operateTime = DateTime.Now.AddMonths(-1);
  169. }
  170. else if (keepTime == "3")//保留近三个月
  171. {
  172. operateTime = DateTime.Now.AddMonths(-3);
  173. }
  174. _sysLogRepository.Deleteable().Where(it => it.LogTime <= operateTime && it.TypeNum == LogType).ExecuteCommand();
  175. }
  176. catch (Exception ex)
  177. {
  178. throw ex;
  179. }
  180. }
  181. public PagedInfo<SysLogDto> GetPageList(Pagination pagination, SysLogQueryDto sysLogQueryDto)
  182. {
  183. var predicate = Expressionable.Create<SysLog>();
  184. // predicate = predicate.AndIF(sysLogQueryDto != null && !string.IsNullOrEmpty(billDocsinfoQueryDto?.KeyWord), m => m.Code.Contains(billDocsinfoQueryDto.KeyWord) || m.Name.Contains(billDocsinfoQueryDto.KeyWord));
  185. var list = _sysLogRepository.Queryable().Where(predicate.ToExpression())
  186. .ToPage<SysLog, SysLogDto>(pagination);
  187. return list;
  188. }
  189. }
  190. }