Redesign LoggerSinkConfiguration.Wrap()
, and introduce LoggerSinkConfiguration.CreateSink()
#2060
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.
Serilog sinks push for factory methods for sink construction and composition (
WriteTo.File()
), rather than an "OO" style of public sink types and constructors (new FileSink(...)
).The existing
LoggerSinkConfiguration.Wrap()
method is a helper used by wrapping sink implementations, such as Serilog.Sinks.Async, and the coreWriteTo.Conditional()
method, to enclose one or more sinks in a "wrapper" that modifies their behavior in some way.The existing
Wrap()
method has three roles: providing access to the enclosed sinks created via their factory methods, delegatingIDisposable.Dispose()
to the enclosed sinks so that wrappers don't need to implement it explicitly, and applying aLoggingLevelSwitch
/minimum level to the completed result.This PR pulls out the two main responsibilities, so that it's now possible to create an
ILogEventSink
without constructing the full logger pipeline (previously only possible using a hack based on theWrap()
call), and it's separately possible to wrap a given sink instance.Creating sinks is performed using
LoggerSinkConfiguration.CreateSink()
:Wrapping uses a new overload of
LoggerSinkConfiguration.Wrap()
:Although it would be possible to simplify this further into
Wrap(sink, wrapper)
, i.e. fully-construct both sink and wrapper, and then just perform disposal delegation inWrap()
:WriteTo
callback anyway, andFinally, the
LoggingLevelSwitch
and minimum level responsibilities are now handed off toWriteTo.Sink()
. SinceCreateSink()
andWrap()
just returnILogEventSink
, there's no need for either of those to have any interaction with levelling.Why make these changes now? With the introduction of
WriteTo.Batched()
, code that previously relied on constructingPeriodicBatchingSink
will be ported over to it. Unlike the older API, no sink class is exposed - batching is only available through the factory method. This makes porting nontrivial usage of PBS harder.CreateSink()
fixes this.