LogHub.cs 4.6 KB

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