LogEngineImplement.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Flyinsono.Client.Test.Utilities.Executors;
  7. namespace Flyinsono.Client.Test.Log
  8. {
  9. public class LogItem
  10. {
  11. /// <summary>
  12. /// Log level
  13. /// </summary>
  14. public LogLevel LogLevel { get; set; }
  15. /// <summary>
  16. /// Log content
  17. /// </summary>
  18. public string Content { get; set; }
  19. }
  20. public class LogEngineImplement : DefaultLogEngine
  21. {
  22. private SequenceExecutor<string> _logSequenceExecutor = new SequenceExecutor<string>("LogSequenceExecutor");
  23. private DateTime _today;
  24. Action<LogItem> _actionShowLogger;
  25. public LogEngineImplement(Action<LogItem> action)
  26. {
  27. _actionShowLogger = action;
  28. }
  29. public override void Write(LogLevel level, string msg)
  30. {
  31. var message = RecombinateMessage(level, msg);
  32. _logSequenceExecutor.Add(ExecuteWrite, message);
  33. _actionShowLogger?.Invoke(new LogItem() { LogLevel = level, Content = message });
  34. }
  35. private bool ExecuteWrite(string message)
  36. {
  37. try
  38. {
  39. if (DateTime.Today != _today)
  40. {
  41. InitializeLogger();
  42. _today = DateTime.Today;
  43. }
  44. WriteMessage(message);
  45. }
  46. catch
  47. {
  48. }
  49. return true;
  50. }
  51. public override void Dispose()
  52. {
  53. _logSequenceExecutor.Dispose();
  54. base.Dispose();
  55. }
  56. }
  57. }