LogHelper.cs 3.6 KB

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