mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Optimalization log processor
This commit is contained in:
parent
91476907f0
commit
7ec3257eac
|
@ -7,7 +7,7 @@ public static class BasedSerilog
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Default output template with the extra properties that can be used for serilog sinks.
|
/// Default output template with the extra properties that can be used for serilog sinks.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
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)
|
public static LoggerConfiguration UseBasedExtension(this LoggerConfiguration loggerConfiguration)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
namespace DotBased.Logging;
|
namespace DotBased.Logging;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -7,21 +9,16 @@ public class LogProcessor : IDisposable
|
||||||
{
|
{
|
||||||
public LogProcessor()
|
public LogProcessor()
|
||||||
{
|
{
|
||||||
_processorQueue = new Queue<LogCapsule>();
|
_canLog = true;
|
||||||
|
_capsuleCollection = new BlockingCollection<LogCapsule>();
|
||||||
IncomingLogHandlerEvent = IncomingLogHandler;
|
IncomingLogHandlerEvent = IncomingLogHandler;
|
||||||
_processorThread = new Thread(ProcessLog)
|
_processTask = Task.Factory.StartNew(ProcessLog);
|
||||||
{
|
|
||||||
IsBackground = true,
|
|
||||||
Name = "Log processor thread (DotBased)"
|
|
||||||
};
|
|
||||||
_processorThread.Start();
|
|
||||||
}
|
}
|
||||||
public readonly Action<LogCapsule> IncomingLogHandlerEvent;
|
public readonly Action<LogCapsule> IncomingLogHandlerEvent;
|
||||||
public event EventHandler<LogCapsule>? LogProcessed;
|
public event EventHandler<LogCapsule>? LogProcessed;
|
||||||
private readonly Queue<LogCapsule> _processorQueue;
|
private bool _canLog;
|
||||||
private readonly Thread _processorThread;
|
private readonly BlockingCollection<LogCapsule> _capsuleCollection;
|
||||||
private readonly ManualResetEvent _threadSuspendEvent = new ManualResetEvent(false);
|
private readonly Task _processTask;
|
||||||
private readonly ManualResetEvent _threadShutdownEvent = new ManualResetEvent(false);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stop the LogProcessor
|
/// Stop the LogProcessor
|
||||||
|
@ -31,9 +28,9 @@ public class LogProcessor : IDisposable
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
_threadShutdownEvent.Set();
|
_canLog = false;
|
||||||
_threadSuspendEvent.Set();
|
_capsuleCollection.CompleteAdding();
|
||||||
_processorThread.Join();
|
_processTask.Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
@ -43,35 +40,31 @@ public class LogProcessor : IDisposable
|
||||||
|
|
||||||
private void IncomingLogHandler(LogCapsule e)
|
private void IncomingLogHandler(LogCapsule e)
|
||||||
{
|
{
|
||||||
_processorQueue.Enqueue(e);
|
if (!_canLog)
|
||||||
// Check if the thread is running, if not wake up the thread.
|
return;
|
||||||
if (!_threadSuspendEvent.WaitOne(0))
|
if (!_capsuleCollection.TryAdd(e))
|
||||||
_threadSuspendEvent.Set();
|
{
|
||||||
|
_canLog = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessLog()
|
private void ProcessLog()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
while (!_capsuleCollection.IsCompleted)
|
||||||
{
|
{
|
||||||
_threadSuspendEvent.WaitOne(Timeout.Infinite);
|
if (_capsuleCollection.TryTake(out var capsule, Timeout.Infinite))
|
||||||
|
|
||||||
if (_threadShutdownEvent.WaitOne(0))
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (_processorQueue.Count != 0)
|
|
||||||
{
|
{
|
||||||
var capsule = _processorQueue.Dequeue();
|
|
||||||
if (!LogService.CanLog(LogService.Options.Severity, capsule.Severity))
|
if (!LogService.CanLog(LogService.Options.Severity, capsule.Severity))
|
||||||
continue;
|
continue;
|
||||||
if (LogService.FilterSeverityLog(capsule))
|
if (LogService.FilterSeverityLog(capsule))
|
||||||
LogProcessed?.Invoke(this, capsule);
|
LogProcessed?.Invoke(this, capsule);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
_threadSuspendEvent.Reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (InvalidOperationException)
|
||||||
|
{ }
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// Write exception to the output
|
// Write exception to the output
|
||||||
|
@ -81,7 +74,8 @@ public class LogProcessor : IDisposable
|
||||||
Console.ForegroundColor = ConsoleColor.Blue;
|
Console.ForegroundColor = ConsoleColor.Blue;
|
||||||
Console.Write($"[{DateTime.Now}] ");
|
Console.Write($"[{DateTime.Now}] ");
|
||||||
Console.ForegroundColor = oldColor;
|
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.ForegroundColor = ConsoleColor.Red;
|
||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user