mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-31 09:18:38 +00:00
Flush log on exit/crash
This commit is contained in:
parent
ecdf5fa1f6
commit
aadf3248fc
1 changed files with 28 additions and 14 deletions
|
@ -1,4 +1,5 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
@ -36,20 +37,6 @@ public static class Logger
|
||||||
const int depth = 5;
|
const int depth = 5;
|
||||||
const int skipDepth = depth - 2;
|
const int skipDepth = depth - 2;
|
||||||
|
|
||||||
// // 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 ", ":");
|
|
||||||
|
|
||||||
StackTrace stackTrace = new(true);
|
StackTrace stackTrace = new(true);
|
||||||
StackFrame? frame = stackTrace.GetFrame(skipDepth + extraTraceLines);
|
StackFrame? frame = stackTrace.GetFrame(skipDepth + extraTraceLines);
|
||||||
Debug.Assert(frame != null);
|
Debug.Assert(frame != null);
|
||||||
|
@ -118,6 +105,33 @@ public static class Logger
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Flush the log queue when we're exiting.
|
||||||
|
AppDomain.CurrentDomain.UnhandledException += Flush;
|
||||||
|
AppDomain.CurrentDomain.ProcessExit += Flush;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logs everything in the queue to all loggers immediately.
|
||||||
|
/// This is a helper function to allow for this function to be easily added to events.
|
||||||
|
/// </summary>
|
||||||
|
public static void Flush(object? _, EventArgs __)
|
||||||
|
{
|
||||||
|
Flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logs everything in the queue to all loggers immediately.
|
||||||
|
/// </summary>
|
||||||
|
public static void Flush()
|
||||||
|
{
|
||||||
|
while (logQueue.TryDequeue(out LogLine line))
|
||||||
|
{
|
||||||
|
foreach (ILogger logger in loggers)
|
||||||
|
{
|
||||||
|
logger.Log(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue