Prechádzať zdrojové kódy

Add custom trace listener for logging

Bepis 6 rokov pred
rodič
commit
df97666312

+ 1 - 0
BepInEx/BepInEx.csproj

@@ -70,6 +70,7 @@
     <Compile Include="Logger\BaseLogger.cs" />
     <Compile Include="Logger\LogLevel.cs" />
     <Compile Include="Logger\PreloaderLogWriter.cs" />
+    <Compile Include="Logger\LoggerTraceListener.cs" />
     <Compile Include="Logger\UnityLogWriter.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Bootstrap\TypeLoader.cs" />

+ 54 - 0
BepInEx/Logger/LoggerTraceListener.cs

@@ -0,0 +1,54 @@
+using System.Diagnostics;
+
+namespace BepInEx.Logger
+{
+    public class LoggerTraceListener : TraceListener
+    {
+        public BaseLogger Logger;
+
+        public LoggerTraceListener(BaseLogger logger)
+        {
+            Logger = logger;
+        }
+
+        public override void Write(string message)
+        {
+            Logger.Write(message);
+        }
+
+        public override void WriteLine(string message)
+        {
+            Logger.WriteLine(message);
+        }
+
+        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
+            => TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
+
+        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
+        {
+            LogLevel level;
+
+            switch (eventType)
+            {
+                case TraceEventType.Critical:
+                    level = LogLevel.Fatal;
+                    break;
+                case TraceEventType.Error:
+                    level = LogLevel.Error;
+                    break;
+                case TraceEventType.Warning:
+                    level = LogLevel.Warning;
+                    break;
+                case TraceEventType.Information:
+                    level = LogLevel.Info;
+                    break;
+                case TraceEventType.Verbose:
+                default:
+                    level = LogLevel.Debug;
+                    break;
+            }
+
+            Logger.Log(level, $"{source} : {message}");
+        }
+    }
+}

+ 6 - 3
BepInEx/Logger/PreloaderLogWriter.cs

@@ -10,7 +10,7 @@ namespace BepInEx.Logger
         public StringBuilder StringBuilder = new StringBuilder();
 
         protected TextWriter stdout;
-        protected TextWriterTraceListener traceListener;
+        protected LoggerTraceListener traceListener;
 
         private bool _enabled = false;
         public bool Enabled {
@@ -27,7 +27,7 @@ namespace BepInEx.Logger
         public PreloaderLogWriter()
         {
             stdout = Console.Out;
-            traceListener = new TextWriterTraceListener(this, "Preloader");
+            traceListener = new LoggerTraceListener(this);
         }
 
         public void Enable()
@@ -62,15 +62,18 @@ namespace BepInEx.Logger
             StringBuilder.Append(value);
         }
 
-        public override void Close()
+        protected override void Dispose(bool disposing)
         {
             Disable();
             StringBuilder.Length = 0;
+
+            base.Dispose(disposing);
         }
 
         public override string ToString()
         {
             return StringBuilder.ToString().Trim();
+
         }
     }
 }