LogHub.cs 10 KB

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