Ltc.cs 6.8 KB

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