Ltc.cs 6.7 KB

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