LogHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Linq.Expressions;
  5. using System.Threading;
  6. namespace WCS.Core
  7. {
  8. /// <summary>
  9. /// Rdeis 数据上抛
  10. /// </summary>
  11. public static class Ltc
  12. {
  13. private static readonly ConcurrentDictionary<Thread, string> Channels = new();
  14. public static void SetChannel(string channel)
  15. {
  16. Channels[Thread.CurrentThread] = channel;
  17. }
  18. public static string GetChannel()
  19. {
  20. return Channels[Thread.CurrentThread];
  21. }
  22. public static void Log(string msg)
  23. {
  24. var channel = GetChannel();
  25. if (channel == "刷新")
  26. {
  27. return;
  28. }
  29. DebugPublisher.Publish(GetChannel(), msg);
  30. }
  31. private static string ResultString<T>(T obj)
  32. {
  33. if (obj == null)
  34. {
  35. return "null";
  36. }
  37. switch (obj)
  38. {
  39. case bool:
  40. {
  41. var b = obj as bool?;
  42. return b.Value ? "成立" : "不成立";
  43. }
  44. case ICollection collection:
  45. {
  46. return collection.Count + "元素";
  47. }
  48. }
  49. return obj.ToString();
  50. }
  51. public static T Do<T>(Expression<Func<T>> exp)
  52. {
  53. var msg = exp.ExpToString();
  54. msg += " 结果:";
  55. try
  56. {
  57. var res = exp.Compile().Invoke();
  58. return res;
  59. }
  60. catch (Exception ex)
  61. {
  62. msg += ex.GetBaseException().Message;
  63. throw;
  64. }
  65. finally
  66. {
  67. Log(msg);
  68. }
  69. }
  70. public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp)
  71. {
  72. var msg = "";
  73. try
  74. {
  75. try
  76. {
  77. msg = exp.ExpToString();
  78. }
  79. catch (Exception)
  80. {
  81. //TODO:日志记录
  82. }
  83. msg += " 结果:";
  84. var res = exp.Compile().Invoke(obj);
  85. msg += ResultString(res);
  86. return res;
  87. }
  88. catch (Exception ex)
  89. {
  90. msg += ex.GetBaseException().Message;
  91. throw;
  92. }
  93. finally
  94. {
  95. Log(msg);
  96. }
  97. }
  98. public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
  99. {
  100. var msg = exp.ExpToString();
  101. msg += " 结果:";
  102. try
  103. {
  104. var res = exp.Compile().Invoke(obj, obj2);
  105. msg += ResultString(res);
  106. return res;
  107. }
  108. catch (Exception ex)
  109. {
  110. msg += ex.GetBaseException().Message;
  111. throw;
  112. }
  113. finally
  114. {
  115. Log(msg);
  116. }
  117. }
  118. public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
  119. {
  120. var msg = exp.ExpToString();
  121. msg += " 结果:";
  122. try
  123. {
  124. var res = exp.Compile().Invoke(obj, obj2, obj3);
  125. msg += ResultString(res);
  126. return res;
  127. }
  128. catch (Exception ex)
  129. {
  130. msg += ex.GetBaseException().Message;
  131. throw;
  132. }
  133. finally
  134. {
  135. Log(msg);
  136. }
  137. }
  138. }
  139. }