LogHub.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Newtonsoft.Json;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Redis;
  4. using System.Collections.Concurrent;
  5. namespace ServiceCenter.Logs
  6. {
  7. public class LogHub
  8. {
  9. /// <summary>
  10. /// 发布一条世界交互日志
  11. /// </summary>
  12. /// <param name="logs"></param>
  13. public static void WorldPublish(ConcurrentQueue<KeyLog> logs)
  14. {
  15. if (logs.Count > 0)
  16. {
  17. //每一条数据存入redis
  18. foreach (var log in logs)
  19. {
  20. var dir = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\{log.Channel.World}\\{log.Channel.System}\\{log.Channel.Item}\\";
  21. var msg = $"{log.Time.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{log}\n";
  22. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  23. {
  24. path = dir,
  25. Title = $"{log.Log.Message.Split(":")[0]}.txt",
  26. Con = msg
  27. }));
  28. }
  29. //存入业务报警内容
  30. List<BusinessAlarm> businesses = logs.Where(v => v.Log.Level != LogLevelEnum.Low).Select(v => new BusinessAlarm()
  31. {
  32. BusinessName = v.Channel.System,
  33. DevNo = v.Channel.Item,
  34. Con = v.ToString(),
  35. Time = DateTime.Now
  36. }).ToList();
  37. RedisHub.WMS.Set($"{nameof(BusinessAlarm)}", JsonConvert.SerializeObject(businesses));
  38. }
  39. }
  40. /// <summary>
  41. /// 发布一条日志记录
  42. /// </summary>
  43. /// <param name="title">接口名</param>
  44. /// <param name="con">内容</param>
  45. public static void InterfacePublish(string title, string con)
  46. {
  47. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  48. {
  49. path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口日志\\",
  50. Title = $"{title}.txt",
  51. Con = $"{con}\n"
  52. }));
  53. }
  54. /// <summary>
  55. /// 增加一条处理日志
  56. /// </summary>
  57. /// <param name="title">文件名</param>
  58. /// <param name="con">内容</param>
  59. public static void InterfaceProcessLog(string title, string con)
  60. {
  61. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  62. {
  63. path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口处理日志",
  64. Title = $"{title}.txt",
  65. Con = $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{con}\n"
  66. }));
  67. }
  68. public static void init()
  69. {
  70. while (true)
  71. {
  72. var logHub = RedisHub.Default.LPop("LogHub");
  73. LogModel log = new LogModel();
  74. try
  75. {
  76. if (logHub != null)
  77. {
  78. log = JsonConvert.DeserializeObject<LogModel>(logHub);
  79. if (log != null)
  80. {
  81. if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  82. //FileInfo fileInfo = new FileInfo(Path.Combine(log.path, log.Title));
  83. //if (fileInfo.Length > 5000)
  84. //{
  85. // var title = log.Title.Split(".");
  86. // File.Move(log.Title, title[0] + "" + title[1]);
  87. //}
  88. File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  89. }
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. var path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  95. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  96. File.AppendAllText(Path.Combine($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程", "Error.txt"), $"{JsonConvert.SerializeObject(log)}--{ex.Message}--{ex.StackTrace}\n");
  97. }
  98. }
  99. }
  100. }
  101. public class LogModel
  102. {
  103. public string path { get; set; }
  104. public string Title { get; set; }
  105. public string Con { get; set; }
  106. }
  107. /// <summary>
  108. /// 业务报警
  109. /// </summary>
  110. public class BusinessAlarm
  111. {
  112. /// <summary>
  113. /// 业务名称
  114. /// </summary>
  115. public string BusinessName { get; set; }
  116. /// <summary>
  117. /// 设备号
  118. /// </summary>
  119. public string DevNo { get; set; }
  120. /// <summary>
  121. /// 内容
  122. /// </summary>
  123. public string Con { get; set; }
  124. /// <summary>
  125. /// 时间
  126. /// </summary>
  127. public DateTime Time { get; set; }
  128. }
  129. }