Update to Serilog 4.x, remove some allocations - #247
Merged
Conversation
…ry<K,V> or (K,V).
nblumhardt
commented
Mar 17, 2024
| { | ||
| if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured)) | ||
| properties.Add(destructured); | ||
| properties.Add(destructured.Name, destructured.Value); |
Member
Author
There was a problem hiding this comment.
This should be [destructured.Name] = destructured.Value to avoid exceptions on duplicate keys.
sungam3r
reviewed
Mar 26, 2024
| return logLevel switch | ||
| { | ||
| LogLevel.None or LogLevel.Critical => LogEventLevel.Fatal, | ||
| LogLevel.None => LevelAlias.Off, |
Member
Author
There was a problem hiding this comment.
I don't think it'll have any effect in the way it's used, here, since events should never carry None - just makes the code clearer.
sungam3r
reviewed
Mar 26, 2024
| if (_logger.BindProperty(property.Key, property.Value, false, out var bound)) | ||
| properties.Add(bound); | ||
| // Simple micro-optimization for the most common and reliably scalar values; could go further here. | ||
| if (property.Value is null or string or int or long && LogEventProperty.IsValidName(property.Key)) |
Contributor
There was a problem hiding this comment.
I would prefer to use ScalarValue.Null for property.Value is null check.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Updates to Serilog 4.x to elminate a dictionary construction using
LogEvent.UnstableAssembleFromParts().This gets the old
LogInformationbenchmark down from (a whopping) 1.26 KB/iteration to (a whopping) 1.17 KB/iteration.But! The
LogInformationbenchmark is flawed, constructing a number of unrelated objects and doing some structure capturing, making it not representative of the overhead imposed by this library.So, I've renamed it to
Capturing, and its companion toCapturingScoped, and added some new benchmarks that should be closer to real-world average usage conditions.Using Serilog.Extensions.Logging still halves throughput and more than doubles allocations compared with just using Serilog directly, but in real-world terms both setups will have pretty negligible effects on latency or GC load.
Before and After
Before (
main)After
Updated benchmarks
The new benchmarks here compare various MEL + Serilog.Extensions.Logging scenarios to a plain
_log.Information("Hello!")Serilog call.