WriteTo.Sink(IBatchedLogEventSink, BatchingOptions, ...)
#2055
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2053, allowing Serilog.Sinks.PeriodicBatching's API to be frozen in its backwards-compatible shape.
This PR makes writing asynchronous, batched sinks as straightforward as regular
ILogEventSink
. This is achieved with a new interface,IBatchedLogEventSink
, which closely mirrors the one exposed by Serilog.Sinks.PeriodicBatching for the past few years:Implementing a batched sink is easy:
And wiring it in is familiar:
Sink packages that need batching behind-the scenes will wrap up the
WriteTo.Sink()
call in their own configuration methods.Design notes
System.Threading.Channels dependency: this is needed on older platforms; modern .NET has this library in-the-box, leaving Serilog dependency-free on .NET 6, .NET 8, etc.
WriteTo.Sink()
vsWriteTo.BatchedSink()
etc: the choice here is part in preparation for a potentialIAsyncLogEventSink
, which would work in a similar manner, and provide an implementation for aWriteTo.Async()
style of API.BatchingOptions
pass by reference rather than callback: while many .NET Core APIs use anoptions => options.X
style of configuration API, Serilog currently does this only when the callback argument supportsWriteTo.X()
style syntax. Passing theBatchingOptions
object directly should keep the usage of callback arguments in the Serilog API consistent.Migrating from Serilog.Sinks.PeriodicBatching
IBatchedLogEventSink.EmitBatchAsync()
acceptsIReadOnlyCollection<LogEvent>
instead ofIEnumerable<LogEvent>
: this makes it possible to access the batch size without enumeration (or needing less-widely-supported enumeration APIs)IBachedLogEventSink.OnEmptyBatchAsync()
has a default implementation on platforms that support it.BatchingSink
is non-public;WriteTo.Sink()
is used instead of explicitly constructing a wrapper sinkBatchingOptions
calls thePeriod
propertyBufferingTimeLimit