in src/AWS.Logger.Core/Core/AWSLoggerCore.cs [192:232]
public void Flush()
{
if (_cancelStartSource.IsCancellationRequested)
return;
if (!_pendingMessageQueue.IsEmpty || !_repo.IsEmpty)
{
bool lockTaken = false;
try
{
// Ensure only one thread executes the flush operation
System.Threading.Monitor.TryEnter(_flushTriggerEvent, ref lockTaken);
if (lockTaken)
{
_flushCompletedEvent.Reset();
if (_flushTriggerEvent.CurrentCount == 0)
{
_flushTriggerEvent.Release(); // Signal Monitor-Task to start premature flush
}
else
{
// Means that the Background Task is busy, and not yet claimed the previous release (Maybe busy with credentials)
var serviceUrl = GetServiceUrl();
LogLibraryServiceError(new TimeoutException($"Flush Pending - ServiceURL={serviceUrl}, StreamName={_currentStreamName}, PendingMessages={_pendingMessageQueue.Count}, CurrentBatch={_repo.CurrentBatchMessageCount}"), serviceUrl);
}
}
// Waiting for Monitor-Task to complete flush
if (!_flushCompletedEvent.Wait(_config.FlushTimeout, _cancelStartSource.Token))
{
var serviceUrl = GetServiceUrl();
LogLibraryServiceError(new TimeoutException($"Flush Timeout - ServiceURL={serviceUrl}, StreamName={_currentStreamName}, PendingMessages={_pendingMessageQueue.Count}, CurrentBatch={_repo.CurrentBatchMessageCount}"), serviceUrl);
}
}
finally
{
if (lockTaken)
System.Threading.Monitor.Exit(_flushTriggerEvent);
}
}
}