LogHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 ConcurrentDictionary<Thread, string> Channels = new ConcurrentDictionary<Thread, string>();
  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. else if (obj is bool)
  38. {
  39. var b = obj as Boolean?;
  40. return b.Value ? "成立" : "不成立";
  41. }
  42. else if (obj is ICollection)
  43. {
  44. var coll = obj as ICollection;
  45. return coll.Count.ToString() + "元素";
  46. }
  47. return obj.ToString();
  48. }
  49. public static T Do<T>(Expression<Func<T>> exp)
  50. {
  51. var msg = exp.ExpToString();
  52. msg += " 结果:";
  53. try
  54. {
  55. var res = exp.Compile().Invoke();
  56. return res;
  57. }
  58. catch (Exception ex)
  59. {
  60. msg += ex.GetBaseException().Message;
  61. throw;
  62. }
  63. finally
  64. {
  65. Log(msg);
  66. }
  67. }
  68. /// <summary>
  69. /// 判断执行条件是否有效
  70. /// </summary>
  71. /// <typeparam name="T1"></typeparam>
  72. /// <typeparam name="T"></typeparam>
  73. /// <param name="obj">表达式需要用到的传参</param>
  74. /// <param name="exp">表达式</param>
  75. /// <param name="message">焦点</param>
  76. /// <param name="errorMessage"></param>
  77. /// <returns></returns>
  78. public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp,string message,string errorMessage)
  79. {
  80. var msg = "";
  81. try
  82. {
  83. try
  84. {
  85. msg = exp.ExpToString();
  86. }
  87. catch (Exception)
  88. {
  89. //TODO:日志记录
  90. }
  91. msg += " 结果:";
  92. var res = exp.Compile().Invoke(obj);
  93. msg += ResultString(res);
  94. return res;
  95. }
  96. catch (Exception ex)
  97. {
  98. msg += ex.GetBaseException().Message;
  99. throw;
  100. }
  101. finally
  102. {
  103. Log(msg);
  104. }
  105. }
  106. public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
  107. {
  108. var msg = exp.ExpToString();
  109. msg += " 结果:";
  110. try
  111. {
  112. var res = exp.Compile().Invoke(obj, obj2);
  113. msg += ResultString(res);
  114. return res;
  115. }
  116. catch (Exception ex)
  117. {
  118. msg += ex.GetBaseException().Message;
  119. throw;
  120. }
  121. finally
  122. {
  123. Log(msg);
  124. }
  125. }
  126. public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
  127. {
  128. var msg = exp.ExpToString();
  129. msg += " 结果:";
  130. try
  131. {
  132. var res = exp.Compile().Invoke(obj, obj2, obj3);
  133. msg += ResultString(res);
  134. return res;
  135. }
  136. catch (Exception ex)
  137. {
  138. msg += ex.GetBaseException().Message;
  139. throw;
  140. }
  141. finally
  142. {
  143. Log(msg);
  144. }
  145. }
  146. }
  147. }