BillLogService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Newtonsoft.Json;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using WMS.BZModels.Models.KLHC;
  5. using WMS.BZSqlSugar;
  6. using WMS.Info;
  7. using WMS.Util;
  8. namespace WMS.BZServices.KLHC
  9. {
  10. public class BillLogService
  11. {
  12. private readonly Repository<BillLog> _billLogRepository;
  13. public BillLogService(Repository<BillLog> billLogRepository)
  14. {
  15. _billLogRepository = billLogRepository;
  16. }
  17. public void WriteLog(BZBLLExecInfo BLLExec, ELogType eLogType)
  18. {
  19. try
  20. {
  21. BillLog log = new BillLog();
  22. log.RunMs = (int)CommonUtil.TimerEnd(BLLExec.RunWatch);
  23. log.LogTime = DateTime.Now;
  24. log.AppTypeNum = 0;
  25. log.TypeNum = (int)eLogType;
  26. if (BLLExec.Ex == null)
  27. {
  28. log.ContentText = string.IsNullOrWhiteSpace(BLLExec.BLLObj.SuccessMsg) ? "操作成功。" : BLLExec.BLLObj.SuccessMsg;
  29. log.IsSucc = 1;
  30. }
  31. else
  32. {
  33. log.ContentText = BLLExec.Ex.Message;
  34. log.IsSucc = 0;
  35. }
  36. if (BLLExec.BLLObj != null)
  37. {
  38. log.AppTypeNum = (int)BLLExec.BLLObj.LoginUser.AppType;
  39. log.Method = BZBLLCore.GetBLLClassNo(BLLExec.BLLObj);
  40. log.MethodText = BLLExec.BLLObj.BLLDesc;
  41. StringBuilder sb = new StringBuilder();
  42. sb.Append("---------------------------------------请求数据-----------------------------------------------------------------");
  43. sb.AppendLine(BLLExec.BLLObj.InJsonData);
  44. if (BLLExec.Ex != null)
  45. {
  46. sb.AppendLine("----------------------------------------异常数据----------------------------------------------------------------");
  47. sb.AppendLine(BLLExec.Ex.ToString());
  48. sb.AppendLine("----------------------------------------异常内部数据----------------------------------------------------------------");
  49. if (BLLExec.Ex.InnerException == null)
  50. {
  51. sb.AppendLine(string.Format("Message:{0} Source:{1} StackTrace:{2} ", BLLExec.Ex.Message, BLLExec.Ex.Source, BLLExec.Ex.StackTrace));
  52. }
  53. else
  54. {
  55. sb.AppendLine(string.Format("Message:{0} Source:{1} StackTrace:{2} ", BLLExec.Ex.InnerException.Message, BLLExec.Ex.InnerException.Source, BLLExec.Ex.InnerException.StackTrace));
  56. }
  57. }
  58. log.ParamJson = sb.ToString();
  59. log.Ip = BLLExec.BLLObj.LoginUser.IPAddress;
  60. log.LogWho = BLLExec.BLLObj.LoginUser.UserNo;
  61. //log. = BLLExec.BLLObj.LoginUser.AppDeviceNo;
  62. }
  63. _billLogRepository.Insert(log);
  64. }
  65. catch (Exception ex)
  66. {
  67. LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(ex.ToString());
  68. }
  69. }
  70. public void WriteLog(LoginUserInfo LoginUser, dynamic JsonData, string ContentText, string ClassNo, string ClassDesc, Stopwatch ts, ELogType eLogType, Exception ex = null)
  71. {
  72. try
  73. {
  74. string injson = JsonData == null ? "" : JsonConvert.SerializeObject(JsonData);
  75. BillLog log = new BillLog();
  76. log.RunMs = (int)CommonUtil.TimerEnd(ts);
  77. log.LogTime = DateTime.Now;
  78. log.AppTypeNum = (int)LoginUser.AppType;
  79. log.TypeNum = (int)eLogType;
  80. if (!string.IsNullOrWhiteSpace(ContentText))
  81. {
  82. log.ContentText = ContentText;
  83. }
  84. else
  85. {
  86. log.ContentText = ex == null ? "操作成功。" : ex.Message;
  87. }
  88. log.IsSucc = ex == null ? 1 : 0;
  89. log.Ip = LoginUser.IPAddress;
  90. log.Method = ClassNo;
  91. log.MethodText = ClassDesc;
  92. //log.F_APPDEVICENO = LoginUser.AppDeviceNo;
  93. log.LogWho = LoginUser.UserNo;
  94. log.AddWho = LoginUser.UserNo;
  95. log.AddTime = DateTime.Now;
  96. log.EditTime = DateTime.Now;
  97. log.EditWho = LoginUser.UserNo;
  98. log.ParamJson = injson;
  99. _billLogRepository.Insert(log);
  100. }
  101. catch (Exception e)
  102. {
  103. CommonUtil.TimerEnd(ts);
  104. LogFactory.GetLogger("WMS.BZServices.PT.BillLogService.WriteLog").Error(e.ToString());
  105. }
  106. }
  107. }
  108. }