XunitTestLogger.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using Newtonsoft.Json;
  3. using SongVocalSectionAnalyser.Common.Logging;
  4. using SongVocalSectionAnalyser.Common.Logging.JsonConverters;
  5. using SongVocalSectionAnalyser.Common.Logging.Model;
  6. using Xunit.Abstractions;
  7. namespace SongVocalSectionAnalyser.Tests
  8. {
  9. internal class XunitTestLogger : LoggerBase
  10. {
  11. private readonly ITestOutputHelper _output;
  12. public XunitTestLogger(ITestOutputHelper output)
  13. {
  14. _output = output;
  15. }
  16. protected override void WriteLog(LogEntry entry)
  17. {
  18. string dj;
  19. try
  20. {
  21. dj = entry.Details == null ? "" : JsonConvert.SerializeObject(entry.Details, LogJsonSerializerSettings.Default);
  22. }
  23. catch (Exception e)
  24. {
  25. var hold = entry.MakeCopyWithoutDetails();
  26. hold.Details = new { SerializationFail = e.Message, DowngradeToString = entry.Details?.ToString() };
  27. dj = JsonConvert.SerializeObject(hold, LogJsonSerializerSettings.Default);
  28. }
  29. _output.WriteLine($"{entry.Time}|{entry.Level}|{entry.Thread.Id}:{entry.Thread.Name}|{entry.Summary}|{dj}");
  30. }
  31. }
  32. }