LogHub.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections.Concurrent;
  2. using System.IO;
  3. using WCS.Core;
  4. namespace ServiceCenter
  5. {
  6. public class LogHub
  7. {
  8. /// <summary>
  9. /// 日志队列
  10. /// </summary>
  11. public static ConcurrentQueue<LogModel> Logs = new ConcurrentQueue<LogModel>();
  12. /// <summary>
  13. /// 发布一条日志记录
  14. /// </summary>
  15. /// <param name="path">路径</param>
  16. /// <param name="title">文件名</param>
  17. /// <param name="con">内容</param>
  18. public static void Publish(string path, string title, string con)
  19. {
  20. Logs.Enqueue(new LogModel
  21. {
  22. path = path,
  23. Title = title,
  24. Con = con
  25. });
  26. }
  27. public static void init()
  28. {
  29. while (true)
  30. {
  31. foreach (var log in Logs)
  32. {
  33. if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
  34. File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
  35. }
  36. Logs.Clear();
  37. Thread.Sleep(1000);
  38. }
  39. }
  40. }
  41. public class LogModel
  42. {
  43. public string path { get; set; }
  44. public string Title { get; set; }
  45. public string Con { get; set; }
  46. }
  47. }