mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 10:08:39 +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;
|
namespace LBPUnion.ProjectLighthouse.Logging;
|
||||||
|
|
||||||
public struct LogTrace
|
public struct LogTrace
|
||||||
{
|
{
|
||||||
public string Name;
|
public string? Name;
|
||||||
public short Line;
|
public string? Section;
|
||||||
}
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
using System;
|
#nullable enable
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -31,35 +31,51 @@ public static class Logger
|
||||||
LogSuccess("Initialized " + logger.GetType().Name, "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;
|
const int skipDepth = depth - 2;
|
||||||
|
|
||||||
// Get the stacktrace for logging...
|
// // Get the stacktrace for logging...
|
||||||
string trace = Environment.StackTrace.Split('\n', depth, StringSplitOptions.RemoveEmptyEntries).Skip(skipDepth).First();
|
// 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');
|
StackTrace stackTrace = new(true);
|
||||||
trace = trace.Substring(trace.LastIndexOf(Path.DirectorySeparatorChar) + 1);
|
StackFrame? frame = stackTrace.GetFrame(skipDepth + extraTraceLines);
|
||||||
trace = trace.Replace(".cs:line ", ":");
|
Debug.Assert(frame != null);
|
||||||
|
|
||||||
string[] traceSplit = trace.Split(':'); // Split for formatting!
|
string? name;
|
||||||
|
string? section;
|
||||||
|
|
||||||
short lineNumber;
|
string? fileName = frame.GetFileName();
|
||||||
|
if (fileName == null)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
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
|
return new LogTrace
|
||||||
{
|
{
|
||||||
Name = traceSplit[0],
|
Name = name,
|
||||||
Line = lineNumber,
|
Section = section,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +171,7 @@ public static class Logger
|
||||||
Log(text, area, LogLevel.Error);
|
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
|
queueLog
|
||||||
(
|
(
|
||||||
|
@ -164,7 +180,7 @@ public static class Logger
|
||||||
Level = level,
|
Level = level,
|
||||||
Message = text,
|
Message = text,
|
||||||
Area = area,
|
Area = area,
|
||||||
Trace = getTrace(),
|
Trace = getTrace(extraTraceLines),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,10 +21,10 @@ public class AspNetToLighthouseLogger : Microsoft.Extensions.Logging.ILogger
|
||||||
{
|
{
|
||||||
LogLevel level = logLevel.ToLighthouseLevel();
|
LogLevel level = logLevel.ToLighthouseLevel();
|
||||||
|
|
||||||
Logger.Log(state.ToString(), this.Category, level);
|
Logger.Log(state.ToString(), this.Category, level, 4);
|
||||||
|
|
||||||
if (exception == null) return;
|
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)
|
public void Log(LogLine logLine)
|
||||||
{
|
{
|
||||||
ConsoleColor oldBackgroundColor = Console.BackgroundColor;
|
|
||||||
ConsoleColor oldForegroundColor = Console.ForegroundColor;
|
ConsoleColor oldForegroundColor = Console.ForegroundColor;
|
||||||
|
|
||||||
foreach (string line in logLine.Message.Split('\n'))
|
foreach (string line in logLine.Message.Split('\n'))
|
||||||
|
@ -15,28 +14,36 @@ public class ConsoleLogger : ILogger
|
||||||
// The following is scuffed. Beware~
|
// The following is scuffed. Beware~
|
||||||
|
|
||||||
// Write the level! [Success]
|
// Write the level! [Success]
|
||||||
Console.BackgroundColor = logLine.Level.ToColor().ToDark();
|
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Write('[');
|
Console.Write('[');
|
||||||
Console.ForegroundColor = logLine.Level.ToColor();
|
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.Write(logLine.Level);
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Write(']');
|
Console.Write(']');
|
||||||
Console.ForegroundColor = oldForegroundColor;
|
Console.ForegroundColor = oldForegroundColor;
|
||||||
Console.BackgroundColor = oldBackgroundColor;
|
|
||||||
Console.Write(' ');
|
Console.Write(' ');
|
||||||
|
|
||||||
|
if (logLine.Trace.Name != null)
|
||||||
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Write('<');
|
Console.Write('<');
|
||||||
Console.ForegroundColor = logLine.Level.ToColor();
|
Console.ForegroundColor = logLine.Level.ToColor();
|
||||||
Console.Write(logLine.Trace.Name);
|
Console.Write(logLine.Trace.Name);
|
||||||
|
if (logLine.Trace.Section != null)
|
||||||
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Write(':');
|
Console.Write(':');
|
||||||
Console.ForegroundColor = logLine.Level.ToColor();
|
Console.ForegroundColor = logLine.Level.ToColor();
|
||||||
Console.Write(logLine.Trace.Line);
|
Console.Write(logLine.Trace.Section);
|
||||||
|
}
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Write("> ");
|
Console.Write("> ");
|
||||||
Console.ForegroundColor = oldForegroundColor;
|
Console.ForegroundColor = oldForegroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine(line);
|
Console.WriteLine(line);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue