LogHub.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using Newtonsoft.Json;
  2. using PlcSiemens.Core.Extension;
  3. using ServiceCenter.Extensions;
  4. using ServiceCenter.Redis;
  5. using System.Collections.Concurrent;
  6. using System.Diagnostics;
  7. using WCS.Core;
  8. namespace ServiceCenter.Logs
  9. {
  10. public static class LogHub
  11. {
  12. /// <summary>
  13. /// 发布世界交互日志
  14. /// </summary>
  15. /// <param name="logs"></param>
  16. /// <param name="World">当前世界</param>
  17. public static void WorldPublish(ConcurrentQueue<KeyLog> logs, string World)
  18. {
  19. if (logs.Count <= 0) return;
  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 => new
  30. {
  31. x.Time,
  32. x.Log.Message
  33. }).ToList();
  34. RedisHub.Default.Set($"WCSLog:{item1.Key}:{item2.Key}", JsonConvert.SerializeObject(msgList));
  35. });
  36. });
  37. }
  38. catch
  39. {
  40. // ignored
  41. }
  42. try
  43. {
  44. //每一条数据存入redis
  45. foreach (var log in logs.OrderBy(x => x.Time))
  46. {
  47. try
  48. {
  49. var dir = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\{log.Channel.World}\\{log.Channel.System}\\{log.Channel.Item}\\";
  50. var msg = $"{log.Time.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{log}";
  51. if (log.Log.Message.IsNullOrEmpty()) continue;
  52. var title = $"{log.Log.Message.Split(":")[0]}.txt";
  53. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  54. {
  55. Time = log.Time,
  56. path = dir,
  57. Title = title,
  58. Con = msg
  59. }));
  60. }
  61. catch
  62. {
  63. // ignored
  64. }
  65. }
  66. //存入业务报警内容
  67. List<BusinessAlarm> businesses = logs.Where(v => v.Log.Level > LogLevelEnum.Low && v.Log.LogUpLoad == LogUpLoadEnum.UpLoadWMS).Select(v => new BusinessAlarm()
  68. {
  69. BusinessName = v.Channel.System,
  70. DevNo = v.Channel.Item,
  71. Con = v.ToString(),
  72. Time = DateTime.Now
  73. }).ToList();
  74. RedisHub.WMS.Set($"{nameof(BusinessAlarm)}:{World}", JsonConvert.SerializeObject(businesses));
  75. }
  76. catch
  77. {
  78. // ignored
  79. }
  80. }
  81. /// <summary>
  82. /// 去除转义字符
  83. /// </summary>
  84. /// <param name="value"></param>
  85. /// <returns></returns>
  86. public static string RemoveEscapeCharacters(this string? value)
  87. {
  88. return value.Trim('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v').Trim();
  89. }
  90. public static string GetString(string value)
  91. {
  92. return value.Replace("INSERT INTO ", "")
  93. .Replace(",N'", ",'")
  94. .Replace("\0", "")
  95. .Replace("wcs_", "")
  96. .Replace("(N'", "('") + "\r";
  97. }
  98. /// <summary>
  99. /// 执行记录
  100. /// </summary>
  101. /// <param name="system">系统</param>
  102. /// <param name="devCode">设备号</param>
  103. /// <param name="msg">内容</param>
  104. public static void ExRecord(this SystemBase system, string devCode, string msg)
  105. {
  106. }
  107. /// <summary>
  108. /// 执行记录
  109. /// </summary>
  110. /// <param name="system">系统</param>
  111. /// <param name="devCode">设备号</param>
  112. /// <param name="msg">内容</param>
  113. public static async void ExRecord<T>(this SystemBase system, string devCode, string msg, List<T> ints)
  114. {
  115. try
  116. {
  117. }
  118. catch
  119. {
  120. // ignored
  121. }
  122. }
  123. /// <summary>
  124. /// 发布一条日志记录
  125. /// </summary>
  126. /// <param name="title">接口名</param>
  127. /// <param name="con">内容</param>
  128. public static void InterfacePublish(string title, string con)
  129. {
  130. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  131. {
  132. path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口日志\\",
  133. Title = $"{title}.txt",
  134. Con = $"{DateTime.Now.yyyyMMddhhmmssf()}--{con}"
  135. }));
  136. }
  137. /// <summary>
  138. /// 初始化日志处理进程
  139. /// </summary>
  140. public static void init()
  141. {
  142. var toao = 1000;
  143. while (true)
  144. {
  145. var sw = new Stopwatch();
  146. sw.Start();
  147. var index = 0;
  148. List<LogModel> logs = new List<LogModel>();
  149. try
  150. {
  151. using (var tran = RedisHub.Default.Multi())
  152. {
  153. var a = tran.LRange("LogHub", 0, 19999);
  154. tran.LTrim("LogHub", 20000, -1);
  155. var value = tran.Exec()[0];
  156. var ret = value as string[];
  157. logs = ret.Select(JsonConvert.DeserializeObject<LogModel>).ToList();
  158. }
  159. //Parallel.ForEach(logs.Where(log => log != null).GroupBy(x => x.path), psth =>
  160. //{
  161. //});
  162. foreach (var path in logs.Where(log => log != null).GroupBy(x => x.path))
  163. {
  164. foreach (var title in path.GroupBy(x => x.Title))
  165. {
  166. var log = title.First();
  167. var cons = title.OrderBy(x => x.Time).Select(x => x.Con);
  168. if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  169. File.AppendAllLines(Path.Combine(log.path, log.Title), cons);
  170. index++;
  171. }
  172. }
  173. ////log = ret as LogModel;
  174. //if (log != null)
  175. //{
  176. // if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  177. // File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  178. //}
  179. }
  180. catch (Exception ex)
  181. {
  182. var path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  183. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  184. }
  185. sw.Stop();
  186. var path1 = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  187. if (!Directory.Exists(path1)) Directory.CreateDirectory(path1);
  188. File.AppendAllText(Path.Combine(path1, "耗时.txt"), $"{DateTime.Now.yyyyMMddhhmmssf()}耗时:{sw.ElapsedMilliseconds}--{logs.Count}--{index}\n");
  189. if (sw.ElapsedMilliseconds >= toao) continue;
  190. var time = toao - sw.ElapsedMilliseconds.ToInt();
  191. Thread.Sleep(time);
  192. }
  193. }
  194. }
  195. public class LogModel
  196. {
  197. public string path { get; set; }
  198. public DateTime Time { get; set; }
  199. public string Title { get; set; }
  200. public string Con { get; set; }
  201. }
  202. /// <summary>
  203. /// 业务报警
  204. /// </summary>
  205. public class BusinessAlarm
  206. {
  207. /// <summary>
  208. /// 业务名称
  209. /// </summary>
  210. public string BusinessName { get; set; }
  211. /// <summary>
  212. /// 设备号
  213. /// </summary>
  214. public string DevNo { get; set; }
  215. /// <summary>
  216. /// 内容
  217. /// </summary>
  218. public string Con { get; set; }
  219. /// <summary>
  220. /// 时间
  221. /// </summary>
  222. public DateTime Time { get; set; }
  223. }
  224. }