LogHub.cs 4.4 KB

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