LogHub.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{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. File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  83. }
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. var path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  89. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  90. File.AppendAllText(Path.Combine($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程", "Error.txt"), $"{JsonConvert.SerializeObject(log)}--{ex.Message}--{ex.StackTrace}\n");
  91. }
  92. }
  93. }
  94. }
  95. public class LogModel
  96. {
  97. public string path { get; set; }
  98. public string Title { get; set; }
  99. public string Con { get; set; }
  100. }
  101. /// <summary>
  102. /// 业务报警
  103. /// </summary>
  104. public class BusinessAlarm
  105. {
  106. /// <summary>
  107. /// 业务名称
  108. /// </summary>
  109. public string BusinessName { get; set; }
  110. /// <summary>
  111. /// 设备号
  112. /// </summary>
  113. public string DevNo { get; set; }
  114. /// <summary>
  115. /// 内容
  116. /// </summary>
  117. public string Con { get; set; }
  118. /// <summary>
  119. /// 时间
  120. /// </summary>
  121. public DateTime Time { get; set; }
  122. }
  123. }