diff --git a/MonkeyLoader/Logging/LoggingController.cs b/MonkeyLoader/Logging/LoggingController.cs index b49e277..1ce002e 100644 --- a/MonkeyLoader/Logging/LoggingController.cs +++ b/MonkeyLoader/Logging/LoggingController.cs @@ -18,8 +18,8 @@ public sealed class LoggingController private readonly Timer _flushTimer; private bool _autoFlush = true; - private LoggingHandler _handler = MissingLoggingHandler.Instance; + private Task _lastLogTask = Task.CompletedTask; /// /// Gets or sets whether this logger will automatically trigger flushing of messages. @@ -122,7 +122,7 @@ internal void LogInternal(LoggingLevel level, string identifier, Func me if (!ShouldLog(level)) return; - Task.Run(() => + QueueLogging(() => { LogLevelToLogger(level)(MakeMessageProducer(level, identifier, messageProducer)); @@ -135,7 +135,7 @@ internal void LogInternal(LoggingLevel level, string identifier, IEnumerable + QueueLogging(() => { var logger = LogLevelToLogger(level); @@ -151,7 +151,7 @@ internal void LogInternal(LoggingLevel level, string identifier, IEnumerable + QueueLogging(() => { var logger = LogLevelToLogger(level); @@ -236,6 +236,15 @@ private Func MakeMessageProducer(LoggingLevel level, string identifier, private Func MakeMessageProducer(LoggingLevel level, string identifier, object message) => () => $"{LogLevelToString(level)} [{identifier}] {message}"; + private void QueueLogging(Action handleLogging) + { + lock (this) + { + _lastLogTask = _lastLogTask.ContinueWith(_ => handleLogging(), + TaskContinuationOptions.RunContinuationsAsynchronously); + } + } + private sealed class DeferredMessage { public readonly LoggingLevel LoggingLevel;