LogHub.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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="title">文件名</param>
  46. /// <param name="con">内容</param>
  47. public static void InterfaceProcessLog(string title, string con)
  48. {
  49. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  50. {
  51. path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口处理日志",
  52. Title = $"{title}.txt",
  53. Con = $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{con}\n"
  54. }));
  55. }
  56. public static void init()
  57. {
  58. while (true)
  59. {
  60. var logHub = RedisHub.Default.LPop("LogHub");
  61. LogModel log = new LogModel();
  62. try
  63. {
  64. if (logHub != null)
  65. {
  66. log = JsonConvert.DeserializeObject<LogModel>(logHub);
  67. if (log != null)
  68. {
  69. if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  70. File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  71. }
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. var path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
  77. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  78. File.AppendAllText(Path.Combine($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程", "Error.txt"), $"{JsonConvert.SerializeObject(log)}--{ex.Message}--{ex.StackTrace}\n");
  79. }
  80. }
  81. }
  82. }
  83. public class LogModel
  84. {
  85. public string path { get; set; }
  86. public string Title { get; set; }
  87. public string Con { get; set; }
  88. }
  89. /// <summary>
  90. /// 业务报警
  91. /// </summary>
  92. public class BusinessAlarm
  93. {
  94. /// <summary>
  95. /// 业务名称
  96. /// </summary>
  97. public string BusinessName { get; set; }
  98. /// <summary>
  99. /// 设备号
  100. /// </summary>
  101. public string DevNo { get; set; }
  102. /// <summary>
  103. /// 内容
  104. /// </summary>
  105. public string Con { get; set; }
  106. /// <summary>
  107. /// 时间
  108. /// </summary>
  109. public DateTime Time { get; set; }
  110. }
  111. }