diff --git a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs index ef16e795..29b7d599 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs @@ -6,7 +6,7 @@ using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; namespace SPTarkov.Server.Core.Utils.Logger; [Injectable(TypePriority = int.MinValue)] -public class SptLogger : ISptLogger, IDisposable +public class SptLogger : ISptLogger { private string _category; private readonly SptLoggerQueueManager _loggerQueueManager; @@ -206,9 +206,4 @@ public class SptLogger : ISptLogger, IDisposable { _loggerQueueManager.DumpAndStop(); } - - public void Dispose() - { - _loggerQueueManager.DumpAndStop(); - } } diff --git a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerQueueManager.cs b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerQueueManager.cs index 6b1ff94b..1d3181c2 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerQueueManager.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerQueueManager.cs @@ -1,3 +1,5 @@ +using System.Collections.Concurrent; +using Microsoft.Extensions.Logging; using SPTarkov.DI.Annotations; namespace SPTarkov.Server.Core.Utils.Logger; @@ -10,8 +12,7 @@ public class SptLoggerQueueManager(IEnumerable logHandlers) private Thread? _loggerTask; private readonly Lock LoggerTaskLock = new(); private readonly CancellationTokenSource _loggerCancellationTokens = new(); - private readonly Queue _messageQueue = new(); - private readonly Lock _messageQueueLock = new(); + private readonly BlockingCollection _messageQueue = new(); private Dictionary? _logHandlers; private SptLoggerConfiguration _config; @@ -33,32 +34,19 @@ public class SptLoggerQueueManager(IEnumerable logHandlers) private void LoggerWorkerThread() { - while (!_loggerCancellationTokens.IsCancellationRequested) + while (!_loggerCancellationTokens.Token.IsCancellationRequested) { - lock (_messageQueueLock) + try { - if (_messageQueue.Count != 0) - { - while (_messageQueue.TryDequeue(out var message)) - { - LogMessage(message); - } - } - } - - Thread.Sleep((int)_config.PoolingTimeMs); - } - - lock (_messageQueueLock) - { - // make sure after cancellation that no messages are outstanding - if (_messageQueue.Count != 0) - { - while (_messageQueue.TryDequeue(out var message)) + foreach (var message in _messageQueue.GetConsumingEnumerable(_loggerCancellationTokens.Token)) { LogMessage(message); } } + catch (Exception ex) + { + Console.WriteLine($"Logger queue caught exception: {ex}"); + } } } @@ -106,10 +94,7 @@ public class SptLoggerQueueManager(IEnumerable logHandlers) public void EnqueueMessage(SptLogMessage message) { - lock (_messageQueueLock) - { - _messageQueue.Enqueue(message); - } + _messageQueue.TryAdd(message); } public void DumpAndStop()