Ltc.cs 6.6 KB

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