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
Optimize AddPropertyIfAbsent #1872
Conversation
@@ -14,7 +14,7 @@ | |||
|
|||
namespace Serilog.Capturing; | |||
|
|||
class MessageTemplateProcessor : ILogEventPropertyFactory | |||
class MessageTemplateProcessor : ILogEventPropertyFactory, ILogEventPropertyValueFactory |
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 think this interface was just missed.
src/Serilog/Events/LogEvent.cs
Outdated
@@ -192,3 +194,18 @@ internal LogEvent Copy() | |||
properties); | |||
} | |||
} | |||
|
|||
internal static class LogEventExtensions |
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.
@nblumhardt For now it's internal
but it may be changed to public for use in dependent packages. For example, in serilog-extensions-logging, see EnrichAndCreateScopeItem
:
var property = propertyFactory.CreateProperty(key, value, destructureObject);
logEvent.AddPropertyIfAbsent(property);
src/Serilog/Events/LogEvent.cs
Outdated
|
||
internal static class LogEventExtensions | ||
{ | ||
public static void AddPropertyIfAbsent(this LogEvent evt, ILogEventPropertyFactory factory, string name, object? value, bool destructureObjects = false) |
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.
Since we already end up adding an internal
property to LogEvent
, we could just skip that and make this overload of AddPropertyIfAbsent
an internal method on LogEvent
itself, for now?
I can see how dependent libraries would benefit from making this accessible, but in that case we'd have to expose something on LogEvent
anyway, so we might as well keep everything tight/encapsulated for now.
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.
done
No actual API changes, only a few fixes for NRT annotations.
Eliminates allocation of
LogEventProperty
wrapper forLogEventPropertyValue
.dotnet test -c Release -f net7.0 --filter "FullyQualifiedName=Serilog.PerformanceTests.Harness.LogContextEnrichment"
before
after
See
Allocated
column - 18% less heap allocation for case when event is logged with some pushed property.