LogHub.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using Newtonsoft.Json;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Redis;
  4. using System.Collections.Concurrent;
  5. using System.Diagnostics;
  6. using WCS.Core;
  7. namespace ServiceCenter.Logs
  8. {
  9. public static class LogHub
  10. {
  11. /// <summary>
  12. /// 发布世界交互日志
  13. /// </summary>
  14. /// <param name="logs"></param>
  15. /// <param name="World">当前世界</param>
  16. public static void WorldPublish(ConcurrentQueue<KeyLog> logs, string World)
  17. {
  18. if (logs.Count > 0)
  19. {
  20. //var sql = new StringBuilder();
  21. //sql.Append("INSERT INTO ");
  22. //var db = new SqlSugarHelper().PLC;
  23. try
  24. {
  25. Parallel.ForEach(logs.GroupBy(x => x.Channel.System), item1 =>
  26. {
  27. Parallel.ForEach(item1.GroupBy(x => x.Channel.Item), item2 =>
  28. {
  29. var msgList = item2.Select(x => x.Log.Message).ToList();
  30. RedisHub.Default.Set($"WCSLog:{item1.Key}:{item2.Key}", JsonConvert.SerializeObject(msgList));
  31. });
  32. });
  33. //每一条数据存入redis
  34. foreach (var log in logs)
  35. {
  36. try
  37. {
  38. var dir = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\{log.Channel.World}\\{log.Channel.System}\\{log.Channel.Item}\\";
  39. var msg = $"{log.Time.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{log}\n";
  40. var title = $"{log.Log.Message.Split(":")[0]}.txt";
  41. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  42. {
  43. path = dir,
  44. Title = title,
  45. Con = msg
  46. }));
  47. //sql.Append(GetString(db.Insertable(new WCS_Log()
  48. //{
  49. // World = log.Channel.World,
  50. // System = log.Channel.System,
  51. // Code = log.Channel.Item.ToLower().Replace(".", ""),
  52. // Title = log.Log.Message.Split(":")[0],
  53. // ExTime = log.Time,
  54. // ManagedThreadId = Thread.CurrentThread.ManagedThreadId,
  55. // Content = log.Log.Message.RemoveEscapeCharacters().Substring(0, 254)
  56. //}).ToSqlString()));
  57. }
  58. catch (Exception e)
  59. {
  60. }
  61. }
  62. //var sqlText = sql.ToString();
  63. //if (sqlText.Contains("russ1"))
  64. //{
  65. // var a = 1;
  66. //}
  67. //db.Ado.ExecuteCommand(sql.ToString());
  68. //存入业务报警内容
  69. List<BusinessAlarm> businesses = logs.Where(v => v.Log.Level > LogLevelEnum.Low && v.Log.LogUpLoad == LogUpLoadEnum.UpLoadWMS).Select(v => new BusinessAlarm()
  70. {
  71. BusinessName = v.Channel.System,
  72. DevNo = v.Channel.Item,
  73. Con = v.ToString(),
  74. Time = DateTime.Now
  75. }).ToList();
  76. RedisHub.WMS.Set($"{nameof(BusinessAlarm)}:{World}", JsonConvert.SerializeObject(businesses));
  77. }
  78. catch
  79. {
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// 去除转义字符
  85. /// </summary>
  86. /// <param name="value"></param>
  87. /// <returns></returns>
  88. public static string RemoveEscapeCharacters(this string? value)
  89. {
  90. return value.Trim('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v').Trim();
  91. }
  92. public static string GetString(string value)
  93. {
  94. return value.Replace("INSERT INTO ", "")
  95. .Replace(",N'", ",'")
  96. .Replace("\0", "")
  97. .Replace("wcs_", "")
  98. .Replace("(N'", "('") + "\r";
  99. }
  100. /// <summary>
  101. /// 执行记录
  102. /// </summary>
  103. /// <param name="system">系统</param>
  104. /// <param name="devCode">设备号</param>
  105. /// <param name="msg">内容</param>
  106. public static void ExRecord(this SystemBase system, string devCode, string msg)
  107. {
  108. //var key = $"{system.World.Description}:{devCode}";
  109. //RedisHub.Default.RPush($"{system.World.Description}:{devCode}", msg);
  110. //if (RedisHub.Monitor.LLen(key) > 5000)
  111. //{
  112. // RedisHub.Monitor.LTrim(key, 4000, -1);
  113. //}
  114. }
  115. /// <summary>
  116. /// 执行记录
  117. /// </summary>
  118. /// <param name="system">系统</param>
  119. /// <param name="devCode">设备号</param>
  120. /// <param name="msg">内容</param>
  121. public static async void ExRecord<T>(this SystemBase system, string devCode, string msg, List<T> ints)
  122. {
  123. try
  124. {
  125. //var key = $"{system.World.Description}:{devCode}";
  126. //RedisHub.Default.RPush(key, msg + ints.JsonToString());
  127. //if (RedisHub.Monitor.LLen(key) > 5000)
  128. //{
  129. // RedisHub.Monitor.LTrim(key, 4000, -1);
  130. //}
  131. }
  132. catch (Exception e)
  133. {
  134. //Console.WriteLine(e);
  135. }
  136. }
  137. /// <summary>
  138. /// 发布一条日志记录
  139. /// </summary>
  140. /// <param name="title">接口名</param>
  141. /// <param name="con">内容</param>
  142. public static void InterfacePublish(string title, string con)
  143. {
  144. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  145. {
  146. path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口日志\\",
  147. Title = $"{title}.txt",
  148. Con = $"{DateTime.Now.yyyyMMddhhmmssf()}--{con}\n"
  149. }));
  150. }
  151. /// <summary>
  152. /// 初始化日志处理进程
  153. /// </summary>
  154. public static void init()
  155. {
  156. while (true)
  157. {
  158. var sw = new Stopwatch();
  159. sw.Start();
  160. LogModel log = new LogModel();
  161. try
  162. {
  163. //var res = RedisHub.Default.LRange("LogHub", 0, 5000).ToList();
  164. //var logHubList = new List<LogModel>();
  165. //Parallel.ForEach(res, x =>
  166. //{
  167. // var log = JsonConvert.DeserializeObject<LogModel>(x);
  168. // if (log != null)
  169. // {
  170. // logHubList.Add(log);
  171. // }
  172. //});
  173. //Parallel.ForEach(logHubList.GroupBy(x => x.path).ToList(), logHub =>
  174. //{
  175. // Parallel.ForEach(logHub.GroupBy(x => x.Title).ToList(), log =>
  176. // {
  177. // foreach (var item in log)
  178. // {
  179. // if (item.Con.IsNullOrEmpty())
  180. // {
  181. // if (!Directory.Exists(item.path)) Directory.CreateDirectory(item.path);
  182. // File.AppendAllText(Path.Combine(item.path, item.Title), item.Con);
  183. // }
  184. // }
  185. // });
  186. //});
  187. //RedisHub.Default.LRem("LogHub", 0, 5000);
  188. //foreach (var logHub in logHubList)
  189. //{
  190. // try
  191. // {
  192. // log = ;
  193. // if (log != null)
  194. // {
  195. // if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  196. // File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  197. // count++;
  198. // }
  199. // }
  200. // catch (Exception e)
  201. // {
  202. // // ignored
  203. // }
  204. //}
  205. var logHub = RedisHub.Default.BLPop("LogHub", 0);
  206. log = JsonConvert.DeserializeObject<LogModel>(logHub);
  207. if (log != null)
  208. {
  209. if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  210. //FileInfo fileInfo = new FileInfo(Path.Combine(log.path, log.Title));
  211. //if (fileInfo.Length > 5000)
  212. //{
  213. // var title = log.Title.Split(".");
  214. // File.Move(log.Title, title[0] + "" + title[1]);
  215. //}
  216. File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. var path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  222. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  223. //File.AppendAllText(Path.Combine($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程", "Error.txt"), $"{JsonConvert.SerializeObject(log)}--{ex.Message}--{ex.StackTrace}\n");
  224. }
  225. sw.Stop();
  226. var path1 = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  227. if (!Directory.Exists(path1)) Directory.CreateDirectory(path1);
  228. File.AppendAllText(Path.Combine(path1, "耗时.txt"), $"耗时:{sw.ElapsedMilliseconds}\n");
  229. }
  230. }
  231. }
  232. public class LogModel
  233. {
  234. public string path { get; set; }
  235. public string Title { get; set; }
  236. public string Con { get; set; }
  237. }
  238. /// <summary>
  239. /// 业务报警
  240. /// </summary>
  241. public class BusinessAlarm
  242. {
  243. /// <summary>
  244. /// 业务名称
  245. /// </summary>
  246. public string BusinessName { get; set; }
  247. /// <summary>
  248. /// 设备号
  249. /// </summary>
  250. public string DevNo { get; set; }
  251. /// <summary>
  252. /// 内容
  253. /// </summary>
  254. public string Con { get; set; }
  255. /// <summary>
  256. /// 时间
  257. /// </summary>
  258. public DateTime Time { get; set; }
  259. }
  260. }