mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-30 00:38:38 +00:00
Add support for logging traces outside of Lighthouse
This commit is contained in:
parent
9fa4ffbc90
commit
1d5d8cb514
4 changed files with 62 additions and 38 deletions
|
@ -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;
|
||||
}
|
|
@ -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),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue