diff --git a/ModTek/Features/Logging/LoggingFeature.cs b/ModTek/Features/Logging/LoggingFeature.cs index df25f0f..b7ab76d 100644 --- a/ModTek/Features/Logging/LoggingFeature.cs +++ b/ModTek/Features/Logging/LoggingFeature.cs @@ -163,19 +163,27 @@ internal static void LogAtLevel(string loggerName, LogLevel logLevel, object mes internal static void Flush() { + var flushEvent = new ManualResetEventSlim(false); + + // this cannot guarantee everything is flushed during a shutdown, chances are good though if (!IsDispatchAvailable(out _)) { var messageDto = new MTLoggerMessageDto(); - messageDto.FlushToDisk = true; + messageDto.FlushToDiskPostEvent = flushEvent; ProcessLoggerMessage(ref messageDto); return; } DispatchStopWatch.Start(); ref var updateDto = ref _queue.AcquireUncommitedOrWait(); + updateDto.FlushToDiskPostEvent = flushEvent; DispatchStopWatch.Stop(); updateDto.Commit(); + + // always wait + // usually caller wants to flush to guarantee debug information on disk + flushEvent.Wait(); } private static bool IsDispatchAvailable(out int currentThreadId) @@ -207,12 +215,19 @@ private static DiagnosticsStackTrace GrabStackTrace() private static void ProcessLoggerMessage(ref MTLoggerMessageDto messageDto) { - _consoleLog?.Append(ref messageDto); + try + { + _consoleLog?.Append(ref messageDto); - _mainLog.Append(ref messageDto); - foreach (var logAppender in _logsAppenders) + _mainLog.Append(ref messageDto); + foreach (var logAppender in _logsAppenders) + { + logAppender.Append(ref messageDto); + } + } + finally { - logAppender.Append(ref messageDto); + messageDto.FlushToDiskPostEvent?.Set(); } } diff --git a/ModTek/Features/Logging/MTLoggerMessageDto.cs b/ModTek/Features/Logging/MTLoggerMessageDto.cs index aab13b6..c751f08 100644 --- a/ModTek/Features/Logging/MTLoggerMessageDto.cs +++ b/ModTek/Features/Logging/MTLoggerMessageDto.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Threading; using HBS.Logging; using UnityEngine; @@ -29,7 +30,8 @@ internal static void GetTimings(out long stopwatchTimestamp, out DateTime dateTi internal IStackTrace Location; internal int ThreadId; // or this is set - internal bool FlushToDisk; + internal bool FlushToDisk => FlushToDiskPostEvent != null; + internal ManualResetEventSlim FlushToDiskPostEvent; public MTLoggerMessageDto() {