Skip to content

Commit

Permalink
Factor out SelfLog calls and improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Jan 1, 2024
1 parent 7983a13 commit 83f9133
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ async Task LoopAsync()
}
catch (Exception ex)
{
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}) failed emitting a batch: {ex}");
WriteToSelfLog("failed emitting a batch", ex);
_batchScheduler.MarkFailure();

if (_batchScheduler.ShouldDropBatch)
{
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}) dropping the current batch");
WriteToSelfLog("dropping the current batch");
_currentBatch.Clear();
}

if (_batchScheduler.ShouldDropQueue)
{
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}) dropping all queued events");
WriteToSelfLog("dropping all queued events");

// Not ideal, uses some CPU capacity unnecessarily and doesn't complete in bounded time. The goal is
// to reduce memory pressure on the client if the server is offline for extended periods. May be
Expand Down Expand Up @@ -194,7 +194,7 @@ async Task LoopAsync()
}
catch (Exception ex)
{
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}) failed emitting a batch during shutdown; dropping remaining queued events: {ex}");
WriteToSelfLog("failed emitting a batch during shutdown; dropping remaining queued events", ex);
}
}

Expand All @@ -207,7 +207,7 @@ async Task<bool> TryWaitToReadAsync(ChannelReader<LogEvent> reader, Task timeout
// read task cancellation exceptions during shutdown, may be some room to improve.
if (completed is { Exception: not null, IsCanceled: false })
{
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}) could not read from queue: {completed.Exception}");
WriteToSelfLog($"could not read from queue: {completed.Exception}");
}

// `Task.IsCompletedSuccessfully` not available in .NET Standard 2.0/Framework.
Expand All @@ -227,7 +227,7 @@ public void Dispose()
{
// E.g. the task was canceled before ever being run, or internally failed and threw
// an unexpected exception.
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}) caught exception during disposal: {ex}");
WriteToSelfLog("caught exception during disposal", ex);
}

(_targetSink as IDisposable)?.Dispose();
Expand All @@ -247,7 +247,7 @@ public async ValueTask DisposeAsync()
{
// E.g. the task was canceled before ever being run, or internally failed and threw
// an unexpected exception.
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}): caught exception during async disposal: {ex}");
WriteToSelfLog("caught exception during async disposal", ex);
}

if (_targetSink is IAsyncDisposable asyncDisposable)
Expand All @@ -270,4 +270,10 @@ void SignalShutdown()
}
}
}

void WriteToSelfLog(string message, Exception? exception = null)
{
var ex = exception != null ? $"{Environment.NewLine}{exception}" : "";
SelfLog.WriteLine($"PeriodicBatchingSink ({_targetSink}): {message}{ex}");
}
}

0 comments on commit 83f9133

Please sign in to comment.