-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to Serilog 4.x, remove some allocations #247
Open
nblumhardt
wants to merge
11
commits into
serilog:dev
Choose a base branch
from
nblumhardt:serilog-4x
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
62b775b
Move AddProperty() out to avoid a closure allocation
nblumhardt f845237
It's not necessary to handle {OriginalFormat} when TState is Dictiona…
nblumhardt 5864a5d
Use LogEvent.UnstableAssembleFromParts() to avoid a dictionary alloc
nblumhardt 1c77935
Update BenchmarkDotNet
nblumhardt 03fcf95
"Improve LogEventBenchmark benchmarks":
nblumhardt bcc5021
Ordinal string comparisons
nblumhardt ccae6ff
Remove some accidental usages of throwing Dictionary<,>.Add()
nblumhardt 7bdddec
Roll back EventId comparison change; the overridden != operator does …
nblumhardt adf275f
Tighter level mapping, though for invalid cases only
nblumhardt 51770bd
Merge branch 'dev' into serilog-4x
nblumhardt 10afe06
Merge branch 'dev' into serilog-4x
nblumhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=enricher/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=stringified/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,30 +93,33 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state | |
{ | ||
string? messageTemplate = null; | ||
|
||
var properties = new List<LogEventProperty>(); | ||
var properties = new Dictionary<string, LogEventPropertyValue>(); | ||
|
||
if (state is IEnumerable<KeyValuePair<string, object>> structure) | ||
{ | ||
foreach (var property in structure) | ||
{ | ||
if (property.Key == SerilogLoggerProvider.OriginalFormatPropertyName && property.Value is string value) | ||
if (property is { Key: SerilogLoggerProvider.OriginalFormatPropertyName, Value: string value }) | ||
{ | ||
messageTemplate = value; | ||
} | ||
else if (property.Key.StartsWith('@')) | ||
{ | ||
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured)) | ||
properties.Add(destructured); | ||
properties[destructured.Name] = destructured.Value; | ||
} | ||
else if (property.Key.StartsWith('$')) | ||
{ | ||
if (_logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified)) | ||
properties.Add(stringified); | ||
properties[stringified.Name] = stringified.Value; | ||
} | ||
else | ||
{ | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 will take a look |
||
properties[property.Key] = new ScalarValue(property.Value); | ||
else if (_logger.BindProperty(property.Key, property.Value, false, out var bound)) | ||
properties[bound.Name] = bound.Value; | ||
} | ||
} | ||
|
||
|
@@ -127,7 +130,7 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state | |
{ | ||
messageTemplate = "{" + stateType.Name + ":l}"; | ||
if (_logger.BindProperty(stateType.Name, AsLoggableValue(state, formatter), false, out var stateTypeProperty)) | ||
properties.Add(stateTypeProperty); | ||
properties[stateTypeProperty.Name] = stateTypeProperty.Value; | ||
} | ||
} | ||
|
||
|
@@ -150,19 +153,20 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state | |
if (propertyName != null) | ||
{ | ||
if (_logger.BindProperty(propertyName, AsLoggableValue(state, formatter!), false, out var property)) | ||
properties.Add(property); | ||
properties[property.Name] = property.Value; | ||
} | ||
} | ||
|
||
// The overridden `!=` operator on this type ignores `Name`. | ||
if (eventId.Id != 0 || eventId.Name != null) | ||
properties.Add(CreateEventIdProperty(eventId)); | ||
properties["EventId"] = CreateEventIdPropertyValue(eventId); | ||
|
||
var (traceId, spanId) = Activity.Current is { } activity ? | ||
(activity.TraceId, activity.SpanId) : | ||
(default(ActivityTraceId), default(ActivitySpanId)); | ||
|
||
var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? ""); | ||
return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties, traceId, spanId); | ||
var parsedTemplate = messageTemplate != null ? MessageTemplateParser.Parse(messageTemplate) : MessageTemplate.Empty; | ||
return LogEvent.UnstableAssembleFromParts(DateTimeOffset.Now, level, exception, parsedTemplate, properties, traceId, spanId); | ||
} | ||
|
||
static object? AsLoggableValue<TState>(TState state, Func<TState, Exception?, string>? formatter) | ||
|
@@ -173,7 +177,7 @@ LogEvent PrepareWrite<TState>(LogEventLevel level, EventId eventId, TState state | |
return stateObj ?? state; | ||
} | ||
|
||
internal static LogEventProperty CreateEventIdProperty(EventId eventId) | ||
internal static StructureValue CreateEventIdPropertyValue(EventId eventId) | ||
{ | ||
var properties = new List<LogEventProperty>(2); | ||
|
||
|
@@ -191,6 +195,6 @@ internal static LogEventProperty CreateEventIdProperty(EventId eventId) | |
properties.Add(new LogEventProperty("Name", new ScalarValue(eventId.Name))); | ||
} | ||
|
||
return new LogEventProperty("EventId", new StructureValue(properties)); | ||
return new StructureValue(properties); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bugfix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.