diff --git a/ProjectLighthouse/Logging/LogTrace.cs b/ProjectLighthouse/Logging/LogTrace.cs index 3e011551..df08b52b 100644 --- a/ProjectLighthouse/Logging/LogTrace.cs +++ b/ProjectLighthouse/Logging/LogTrace.cs @@ -1,7 +1,8 @@ +#nullable enable namespace LBPUnion.ProjectLighthouse.Logging; public struct LogTrace { - public string Name; - public short Line; + public string? Name; + public string? Section; } \ No newline at end of file diff --git a/ProjectLighthouse/Logging/Logger.cs b/ProjectLighthouse/Logging/Logger.cs index 45dc99ec..45283ddb 100644 --- a/ProjectLighthouse/Logging/Logger.cs +++ b/ProjectLighthouse/Logging/Logger.cs @@ -1,9 +1,9 @@ -using System; +#nullable enable using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -31,35 +31,51 @@ public static class Logger LogSuccess("Initialized " + logger.GetType().Name, "Logger"); } - private static LogTrace getTrace() + private static LogTrace getTrace(int extraTraceLines = 0) { - const int depth = 6; + const int depth = 5; const int skipDepth = depth - 2; - // Get the stacktrace for logging... - string trace = Environment.StackTrace.Split('\n', depth, StringSplitOptions.RemoveEmptyEntries).Skip(skipDepth).First(); +// // Get the stacktrace for logging... +// string trace = Environment.StackTrace +// .Split('\n', depth + extraTraceLines, StringSplitOptions.RemoveEmptyEntries) +// .Skip(skipDepth + extraTraceLines) +// .First(); +// +// trace = trace.TrimEnd('\r'); +// trace = trace.Substring(trace.LastIndexOf(Path.DirectorySeparatorChar) + 1); // Try splitting by the filename. +// if (trace.StartsWith(" at ")) // If we still havent split properly... +// { +// trace = trace.Substring(trace.LastIndexOf('.') + 1); // Try splitting by the last dot. +// } +// trace = trace.Replace(".cs:line ", ":"); - trace = trace.TrimEnd('\r'); - trace = trace.Substring(trace.LastIndexOf(Path.DirectorySeparatorChar) + 1); - trace = trace.Replace(".cs:line ", ":"); + StackTrace stackTrace = new(true); + StackFrame? frame = stackTrace.GetFrame(skipDepth + extraTraceLines); + Debug.Assert(frame != null); - string[] traceSplit = trace.Split(':'); // Split for formatting! + string? name; + string? section; - short lineNumber; - - try + string? fileName = frame.GetFileName(); + if (fileName == null) { - lineNumber = short.Parse(traceSplit[1]); + name = frame.GetMethod()?.DeclaringType?.Name; + section = frame.GetMethod()?.Name; } - catch + else { - lineNumber = -1; + name = Path.GetFileNameWithoutExtension(Path.GetFileName(fileName)); + int lineNumber = frame.GetFileLineNumber(); + if (lineNumber == 0) lineNumber = -1; + + section = lineNumber.ToString(); } return new LogTrace { - Name = traceSplit[0], - Line = lineNumber, + Name = name, + Section = section, }; } @@ -155,7 +171,7 @@ public static class Logger Log(text, area, LogLevel.Error); } - public static void Log(string text, string area, LogLevel level) + public static void Log(string text, string area, LogLevel level, int extraTraceLines = 0) { queueLog ( @@ -164,7 +180,7 @@ public static class Logger Level = level, Message = text, Area = area, - Trace = getTrace(), + Trace = getTrace(extraTraceLines), } ); } diff --git a/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs b/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs index 1b3b9815..c450c272 100644 --- a/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs +++ b/ProjectLighthouse/Logging/Loggers/AspNet/AspNetToLighthouseLogger.cs @@ -21,10 +21,10 @@ public class AspNetToLighthouseLogger : Microsoft.Extensions.Logging.ILogger { LogLevel level = logLevel.ToLighthouseLevel(); - Logger.Log(state.ToString(), this.Category, level); + Logger.Log(state.ToString(), this.Category, level, 4); if (exception == null) return; - Logger.Log(exception.ToDetailedException(), this.Category, level); + Logger.Log(exception.ToDetailedException(), this.Category, level, 4); } } \ No newline at end of file diff --git a/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs b/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs index f3ef83f4..e055ad32 100644 --- a/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs +++ b/ProjectLighthouse/Logging/Loggers/ConsoleLogger.cs @@ -7,7 +7,6 @@ public class ConsoleLogger : ILogger { public void Log(LogLine logLine) { - ConsoleColor oldBackgroundColor = Console.BackgroundColor; ConsoleColor oldForegroundColor = Console.ForegroundColor; foreach (string line in logLine.Message.Split('\n')) @@ -15,28 +14,36 @@ public class ConsoleLogger : ILogger // The following is scuffed. Beware~ // Write the level! [Success] - Console.BackgroundColor = logLine.Level.ToColor().ToDark(); Console.ForegroundColor = ConsoleColor.White; Console.Write('['); Console.ForegroundColor = logLine.Level.ToColor(); + Console.Write(logLine.Area); + Console.ForegroundColor = ConsoleColor.White; + Console.Write(':'); + Console.ForegroundColor = logLine.Level.ToColor(); Console.Write(logLine.Level); Console.ForegroundColor = ConsoleColor.White; Console.Write(']'); Console.ForegroundColor = oldForegroundColor; - Console.BackgroundColor = oldBackgroundColor; Console.Write(' '); - Console.ForegroundColor = ConsoleColor.White; - Console.Write('<'); - Console.ForegroundColor = logLine.Level.ToColor(); - Console.Write(logLine.Trace.Name); - Console.ForegroundColor = ConsoleColor.White; - Console.Write(':'); - Console.ForegroundColor = logLine.Level.ToColor(); - Console.Write(logLine.Trace.Line); - Console.ForegroundColor = ConsoleColor.White; - Console.Write("> "); - Console.ForegroundColor = oldForegroundColor; + if (logLine.Trace.Name != null) + { + Console.ForegroundColor = ConsoleColor.White; + Console.Write('<'); + Console.ForegroundColor = logLine.Level.ToColor(); + Console.Write(logLine.Trace.Name); + if (logLine.Trace.Section != null) + { + Console.ForegroundColor = ConsoleColor.White; + Console.Write(':'); + Console.ForegroundColor = logLine.Level.ToColor(); + Console.Write(logLine.Trace.Section); + } + Console.ForegroundColor = ConsoleColor.White; + Console.Write("> "); + Console.ForegroundColor = oldForegroundColor; + } Console.WriteLine(line); }