LogHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp)
  69. {
  70. var msg = "";
  71. try
  72. {
  73. try
  74. {
  75. msg = exp.ExpToString();
  76. }
  77. catch (Exception)
  78. {
  79. //TODO:日志记录
  80. }
  81. msg += " 结果:";
  82. var res = exp.Compile().Invoke(obj);
  83. msg += ResultString(res);
  84. return res;
  85. }
  86. catch (Exception ex)
  87. {
  88. msg += ex.GetBaseException().Message;
  89. throw;
  90. }
  91. finally
  92. {
  93. Log(msg);
  94. }
  95. }
  96. public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
  97. {
  98. var msg = exp.ExpToString();
  99. msg += " 结果:";
  100. try
  101. {
  102. var res = exp.Compile().Invoke(obj, obj2);
  103. msg += ResultString(res);
  104. return res;
  105. }
  106. catch (Exception ex)
  107. {
  108. msg += ex.GetBaseException().Message;
  109. throw;
  110. }
  111. finally
  112. {
  113. Log(msg);
  114. }
  115. }
  116. public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
  117. {
  118. var msg = exp.ExpToString();
  119. msg += " 结果:";
  120. try
  121. {
  122. var res = exp.Compile().Invoke(obj, obj2, obj3);
  123. msg += ResultString(res);
  124. return res;
  125. }
  126. catch (Exception ex)
  127. {
  128. msg += ex.GetBaseException().Message;
  129. throw;
  130. }
  131. finally
  132. {
  133. Log(msg);
  134. }
  135. }
  136. }
  137. }