LoggerTraceListener.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Diagnostics;
  2. namespace BepInEx.Logger
  3. {
  4. public class LoggerTraceListener : TraceListener
  5. {
  6. public BaseLogger Logger;
  7. public LoggerTraceListener(BaseLogger logger)
  8. {
  9. Logger = logger;
  10. }
  11. public override void Write(string message)
  12. {
  13. Logger.Write(message);
  14. }
  15. public override void WriteLine(string message)
  16. {
  17. Logger.WriteLine(message);
  18. }
  19. public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
  20. => TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
  21. public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
  22. {
  23. LogLevel level;
  24. switch (eventType)
  25. {
  26. case TraceEventType.Critical:
  27. level = LogLevel.Fatal;
  28. break;
  29. case TraceEventType.Error:
  30. level = LogLevel.Error;
  31. break;
  32. case TraceEventType.Warning:
  33. level = LogLevel.Warning;
  34. break;
  35. case TraceEventType.Information:
  36. level = LogLevel.Info;
  37. break;
  38. case TraceEventType.Verbose:
  39. default:
  40. level = LogLevel.Debug;
  41. break;
  42. }
  43. Logger.Log(level, $"{source} : {message}");
  44. }
  45. }
  46. }