TraceLogSource.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Diagnostics;
  2. namespace BepInEx.Logging
  3. {
  4. /// <summary>
  5. /// A source that routes all logs from the inbuilt .NET <see cref="Trace"/> API to the BepInEx logging system.
  6. /// </summary>
  7. /// <inheritdoc cref="TraceListener"/>
  8. public class TraceLogSource : TraceListener
  9. {
  10. /// <summary>
  11. /// Whether Trace logs are currently being rerouted.
  12. /// </summary>
  13. public static bool IsListening { get; protected set; } = false;
  14. private static TraceLogSource traceListener;
  15. /// <summary>
  16. /// Creates a new trace log source.
  17. /// </summary>
  18. /// <returns>New log source (or already existing one).</returns>
  19. public static ILogSource CreateSource()
  20. {
  21. if (traceListener == null)
  22. {
  23. traceListener = new TraceLogSource();
  24. Trace.Listeners.Add(traceListener);
  25. IsListening = true;
  26. }
  27. return traceListener.LogSource;
  28. }
  29. /// <summary>
  30. /// Internal log source.
  31. /// </summary>
  32. protected ManualLogSource LogSource { get; }
  33. /// <summary>
  34. /// Creates a new trace log source.
  35. /// </summary>
  36. protected TraceLogSource()
  37. {
  38. LogSource = new ManualLogSource("Trace");
  39. }
  40. /// <summary>
  41. /// Writes a message to the underlying <see cref="ManualLogSource"/> instance.
  42. /// </summary>
  43. /// <param name="message">The message to write.</param>
  44. public override void Write(string message)
  45. {
  46. LogSource.LogInfo(message);
  47. }
  48. /// <summary>
  49. /// Writes a message and a newline to the underlying <see cref="ManualLogSource"/> instance.
  50. /// </summary>
  51. /// <param name="message">The message to write.</param>
  52. public override void WriteLine(string message)
  53. {
  54. LogSource.LogInfo(message);
  55. }
  56. /// <inheritdoc />
  57. public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
  58. => TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
  59. /// <inheritdoc />
  60. public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
  61. {
  62. LogLevel level;
  63. switch (eventType)
  64. {
  65. case TraceEventType.Critical:
  66. level = LogLevel.Fatal;
  67. break;
  68. case TraceEventType.Error:
  69. level = LogLevel.Error;
  70. break;
  71. case TraceEventType.Warning:
  72. level = LogLevel.Warning;
  73. break;
  74. case TraceEventType.Information:
  75. level = LogLevel.Info;
  76. break;
  77. case TraceEventType.Verbose:
  78. default:
  79. level = LogLevel.Debug;
  80. break;
  81. }
  82. LogSource.Log(level, $"{message}".Trim());
  83. }
  84. }
  85. }