Ltc.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using FreeRedis;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Linq.Expressions;
  5. using System.Threading;
  6. using System.Threading.Channels;
  7. namespace WCS.Core
  8. {
  9. public static class Ltc
  10. {
  11. private static ConcurrentDictionary<Thread, Channel> Channels = new ConcurrentDictionary<Thread, Channel>();
  12. public static void SetChannel(Channel channel)
  13. {
  14. Channels[Thread.CurrentThread] = channel;
  15. }
  16. public static Channel GetChannel()
  17. {
  18. return Channels[Thread.CurrentThread];
  19. }
  20. static ConcurrentDictionary<Channel, List<LogInfo>> Msgs = new ConcurrentDictionary<Channel, List<LogInfo>>();
  21. public static void ClearChannel()
  22. {
  23. if (Msgs.TryGetValue(GetChannel(), out var list))
  24. list.Clear();
  25. }
  26. public static string GetLogStr()
  27. {
  28. var channel = GetChannel();
  29. if (Msgs.TryGetValue(channel, out var list))
  30. {
  31. var msg = "-------------------"+channel + "--------------------\n" + string.Join('\n', list.Select(v => $"{string.Join('\n', v)}"));
  32. return msg;
  33. }
  34. else
  35. {
  36. return "";
  37. }
  38. }
  39. public static List<LogInfo> GetLogInfo()
  40. {
  41. var channel = GetChannel();
  42. if (Msgs.TryGetValue(channel, out var list))
  43. {
  44. return list;
  45. }
  46. return new List<LogInfo>();
  47. }
  48. public static void Log(string msg,LogLevel level,ErrorType type)
  49. {
  50. var channel = GetChannel();
  51. if (!Msgs.TryGetValue(channel, out var list))
  52. {
  53. list = new List<LogInfo>();
  54. Msgs[channel] = list;
  55. }
  56. list.Add(new LogInfo { Message = msg, Level = level, Type = type, Channel = channel });
  57. }
  58. public static void Publish(World world)
  59. {
  60. var channel = GetChannel();
  61. if (Msgs.TryGetValue(channel, out var list))
  62. {
  63. var msg = string.Join('\n', list);
  64. world.Ex().Publish(channel, msg);
  65. }
  66. }
  67. private static string ResultString<T>(T obj)
  68. {
  69. if (obj == null)
  70. {
  71. return "null";
  72. }
  73. else if (obj is bool)
  74. {
  75. var b = obj as Boolean?;
  76. return b.Value ? "成立" : "不成立";
  77. }
  78. else if (obj is System.Collections.ICollection)
  79. {
  80. var coll = obj as System.Collections.ICollection;
  81. return coll.Count.ToString() + "元素";
  82. }
  83. return obj.ToString();
  84. }
  85. public static T Do<T>(Expression<Func<T>> exp)
  86. {
  87. var msg = exp.ExpToString();
  88. msg += " 结果:";
  89. try
  90. {
  91. var res = exp.Compile().Invoke();
  92. msg += res;
  93. return res;
  94. }
  95. catch (Exception ex)
  96. {
  97. throw;
  98. }
  99. finally
  100. {
  101. Log(msg, LogLevel.低, ErrorType.已知);
  102. }
  103. }
  104. public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp)
  105. {
  106. var msg = "";
  107. try
  108. {
  109. try
  110. {
  111. msg = exp.ExpToString();
  112. }
  113. catch (Exception ex2)
  114. {
  115. }
  116. msg += " 结果:";
  117. var res = exp.Compile().Invoke(obj);
  118. msg += ResultString(res);
  119. return res;
  120. }
  121. catch (Exception ex)
  122. {
  123. throw;
  124. }
  125. finally
  126. {
  127. Log(msg, LogLevel.低, ErrorType.已知);
  128. }
  129. }
  130. public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
  131. {
  132. var msg = exp.ExpToString();
  133. msg += " 结果:";
  134. try
  135. {
  136. var res = exp.Compile().Invoke(obj, obj2);
  137. msg += ResultString(res);
  138. return res;
  139. }
  140. catch (Exception ex)
  141. {
  142. throw;
  143. }
  144. finally
  145. {
  146. Log(msg, LogLevel.低, ErrorType.已知);
  147. }
  148. }
  149. public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
  150. {
  151. var msg = exp.ExpToString();
  152. msg += " 结果:";
  153. try
  154. {
  155. var res = exp.Compile().Invoke(obj, obj2, obj3);
  156. msg += ResultString(res);
  157. return res;
  158. }
  159. catch (Exception ex)
  160. {
  161. throw;
  162. }
  163. finally
  164. {
  165. Log(msg, LogLevel.低, ErrorType.已知);
  166. }
  167. }
  168. }
  169. public class LogInfo
  170. {
  171. public ErrorType Type { get; set; }
  172. public LogLevel Level { get; set; }
  173. public Channel Channel { get; set; }
  174. public string Message { get; set; } = "";
  175. public override string ToString()
  176. {
  177. return $"类型:{Type},级别:{Level},内容:{Message}";
  178. }
  179. }
  180. public class Channel
  181. {
  182. public string World;
  183. public string System;
  184. public string Item;
  185. public override string ToString()
  186. {
  187. return $"{World}-{System}-{Item}";
  188. }
  189. }
  190. public enum ErrorType
  191. {
  192. 未知 = 0,
  193. 已知 = 1
  194. }
  195. public enum LogLevel
  196. {
  197. 低 = 0,
  198. 中 = 1,
  199. 高 = 2
  200. }
  201. public class KnownException : Exception
  202. {
  203. public LogLevel Level { get; set; }
  204. public KnownException(string msg, LogLevel level) : base(msg)
  205. {
  206. this.Level = level;
  207. }
  208. }
  209. }