LogHub.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Newtonsoft.Json;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Redis;
  4. using System.Collections.Concurrent;
  5. namespace ServiceCenter
  6. {
  7. public class LogHub
  8. {
  9. /// <summary>
  10. /// 日志队列
  11. /// </summary>
  12. public static ConcurrentQueue<LogModel> Logs = new ConcurrentQueue<LogModel>();
  13. /// <summary>
  14. /// 发布一条日志记录
  15. /// </summary>
  16. /// <param name="path">路径</param>
  17. /// <param name="title">文件名</param>
  18. /// <param name="con">内容</param>
  19. public static void Publish(string path, string title, string con)
  20. {
  21. RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
  22. {
  23. path = path,
  24. Title = title,
  25. Con = con
  26. }));
  27. }
  28. /// <summary>
  29. /// 增加一条处理日志
  30. /// </summary>
  31. /// <param name="title">文件名</param>
  32. /// <param name="con">内容</param>
  33. public static void InterfaceProcessLog(string title, string con)
  34. {
  35. Logs.Enqueue(new LogModel
  36. {
  37. path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口处理日志",
  38. Title = $"{title}.txt",
  39. Con = con
  40. });
  41. }
  42. public static void init()
  43. {
  44. while (true)
  45. {
  46. var log = JsonConvert.DeserializeObject<LogModel>(RedisHub.Default.LPop("LogHub"));
  47. if (log != null)
  48. {
  49. if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  50. File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  51. }
  52. }
  53. }
  54. }
  55. public class LogModel
  56. {
  57. public string path { get; set; }
  58. public string Title { get; set; }
  59. public string Con { get; set; }
  60. }
  61. }