Ltc.cs 5.7 KB

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