diff --git a/DotBased.Logging.Serilog/BasedSerilog.cs b/DotBased.Logging.Serilog/BasedSerilog.cs index 9f7106f..3d543fb 100644 --- a/DotBased.Logging.Serilog/BasedSerilog.cs +++ b/DotBased.Logging.Serilog/BasedSerilog.cs @@ -7,7 +7,7 @@ public static class BasedSerilog /// /// Default output template with the extra properties that can be used for serilog sinks. /// - public const string OutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3} - {LoggerName}]{NewLine} {Message:lj}{NewLine}{Exception}"; + public const string OutputTemplate = "[{Timestamp:HH:mm:ss} {Level} - {LoggerName}] {Message:lj}{NewLine}{Exception}"; public static LoggerConfiguration UseBasedExtension(this LoggerConfiguration loggerConfiguration) { diff --git a/DotBased/Logging/LogProcessor.cs b/DotBased/Logging/LogProcessor.cs index 3096afd..9a9ff04 100755 --- a/DotBased/Logging/LogProcessor.cs +++ b/DotBased/Logging/LogProcessor.cs @@ -1,3 +1,5 @@ +using System.Collections.Concurrent; + namespace DotBased.Logging; /// @@ -7,21 +9,16 @@ public class LogProcessor : IDisposable { public LogProcessor() { - _processorQueue = new Queue(); + _canLog = true; + _capsuleCollection = new BlockingCollection(); IncomingLogHandlerEvent = IncomingLogHandler; - _processorThread = new Thread(ProcessLog) - { - IsBackground = true, - Name = "Log processor thread (DotBased)" - }; - _processorThread.Start(); + _processTask = Task.Factory.StartNew(ProcessLog); } public readonly Action IncomingLogHandlerEvent; public event EventHandler? LogProcessed; - private readonly Queue _processorQueue; - private readonly Thread _processorThread; - private readonly ManualResetEvent _threadSuspendEvent = new ManualResetEvent(false); - private readonly ManualResetEvent _threadShutdownEvent = new ManualResetEvent(false); + private bool _canLog; + private readonly BlockingCollection _capsuleCollection; + private readonly Task _processTask; /// /// Stop the LogProcessor @@ -31,9 +28,9 @@ public class LogProcessor : IDisposable /// public void Stop() { - _threadShutdownEvent.Set(); - _threadSuspendEvent.Set(); - _processorThread.Join(); + _canLog = false; + _capsuleCollection.CompleteAdding(); + _processTask.Wait(); } public void Dispose() @@ -43,35 +40,31 @@ public class LogProcessor : IDisposable private void IncomingLogHandler(LogCapsule e) { - _processorQueue.Enqueue(e); - // Check if the thread is running, if not wake up the thread. - if (!_threadSuspendEvent.WaitOne(0)) - _threadSuspendEvent.Set(); + if (!_canLog) + return; + if (!_capsuleCollection.TryAdd(e)) + { + _canLog = false; + } } private void ProcessLog() { try { - while (true) + while (!_capsuleCollection.IsCompleted) { - _threadSuspendEvent.WaitOne(Timeout.Infinite); - - if (_threadShutdownEvent.WaitOne(0)) - break; - - if (_processorQueue.Count != 0) + if (_capsuleCollection.TryTake(out var capsule, Timeout.Infinite)) { - var capsule = _processorQueue.Dequeue(); if (!LogService.CanLog(LogService.Options.Severity, capsule.Severity)) continue; if (LogService.FilterSeverityLog(capsule)) LogProcessed?.Invoke(this, capsule); } - else - _threadSuspendEvent.Reset(); } } + catch (InvalidOperationException) + { } catch (Exception e) { // Write exception to the output @@ -81,7 +74,8 @@ public class LogProcessor : IDisposable Console.ForegroundColor = ConsoleColor.Blue; Console.Write($"[{DateTime.Now}] "); Console.ForegroundColor = oldColor; - Console.WriteLine($"[{nameof(LogProcessor)} (DotBased)] Log processor thread failed! No logs are being processed!"); + Console.WriteLine( + $"[{nameof(LogProcessor)} (DotBased)] Log processor thread failed! No logs are being processed!"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); Console.ForegroundColor = ConsoleColor.Yellow;