LoggingHolder.cs 997 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using PCC2.EventBus;
  2. namespace PCC2.Logging;
  3. internal static class LoggingHolder
  4. {
  5. private static readonly List<LoggingEvent> _held = new();
  6. public static void Init(IAsyncEventBus eventBus) => eventBus.Subscript<LoggingEvent>(AddLog);
  7. private static Task AddLog(LoggingEvent obj)
  8. {
  9. lock (_held)
  10. {
  11. _held.Add(obj);
  12. }
  13. return Task.CompletedTask;
  14. }
  15. public static int Count
  16. {
  17. get
  18. {
  19. lock (_held)
  20. {
  21. return _held.Count;
  22. };
  23. }
  24. }
  25. public static IReadOnlyList<LoggingEvent> Get(LogLevel flags, int skip, int take)
  26. {
  27. IReadOnlyList<LoggingEvent> result;
  28. lock (_held)
  29. {
  30. result = _held.Where(p => flags.HasFlag(p.LogLevel)).Skip(skip).Take(take).ToArray();
  31. }
  32. return result;
  33. }
  34. public static void Clear()
  35. {
  36. lock (_held)
  37. {
  38. _held.Clear();
  39. }
  40. }
  41. }