Ltc.cs 6.5 KB

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