Wrapped enrichers, conditional/leveled enrichers, conditional sinks #1316
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.
What issue does this PR address?
This introduces some new configuration options, in part arising out of #1310.
LoggerEnrichmentConfiguration.Wrap(...)
- mirrorsLoggerSinkConfiguration.Wrap()
, enabling enrichers to be wrapped; the two cases below illustrate its utility; it's public so that e.g. Serilog.Filters.Expressions could implement an expression-basedEnrich.When()
.Enrich.When(condition, configuration)
- apply an enricher only to events that matchcondition
; this will enable the requirement in Levelled enrichers/LoggerEnrichmentConfiguration.Wrap()
#1310 to be met.Enrich.AtLevel(level|switch, configuration)
- apply an enricher only to events at or above a specified level (this differs from the semantics ofAtLevel()
discussed in the linked issue). I initially thoughtFromLevel()
would be more consistent withConsole()
'sstandardErrorFromLevel:
, but theFrom
prefix is confusing alongsideEnrich.FromLogContext()
.WriteTo.Conditional(condition, configuration)
- write events matchingcondition
to a sink. We already have the shortcutrestrictedToMinimumLevel
options on sink methods; this method is similar in spirit, and avoids an inefficient and verboseWriteTo.Logger(Filter...WriteTo)
group. Also a difficult naming problem, I'd initially thought to call thisWhen()
to match the enricher equivalent, but theWriteTo.Noun
pattern is distinct fromEnrich.ActionPhrase
pattern and using the same name here read strangely.Conditional()
is the most noun-y option I could come up with, suggestions appreciated.Of these, the enrichment changes enable possibilities that can't be achieved with Serilog 2.8.0, and I think they're more obviously a win for that.
WriteTo.Conditional()
seems closer to borderline as it's essentially sugar; the efficiency gain (not having to allocate copiedLogEvents
along with theirProperties
dictionaries) tips the scales in favor of including it, IMO.Opinions and suggestions most welcome! :-)
Does this PR introduce a breaking change?
No; minor version bump.
Please check if the PR fulfills these requirements
Other information: