-
Notifications
You must be signed in to change notification settings - Fork 104
Fixes #50 - include named scopes #55
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
Conversation
Switched to an opt-out. The parameter is now |
One other consideration; properties added with The original reason for pushing named properties onto |
@serilog/reviewers-core any thoughts on this? Will merge to dev later today otherwise. Cheers! |
Some changes:
Test method: using (_logger.BeginScope("Some name"))
using (_logger.BeginScope(42))
using (_logger.BeginScope("Formatted {WithValue}", 12345))
using (_logger.BeginScope(new Dictionary<string, object> { ["ViaDictionary"] = 100 }))
{
_logger.LogInformation("Hello from the Index!");
} Produces: |
foreach (var stateProperty in stateProperties) | ||
{ | ||
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string) | ||
{ | ||
scopeItem = new ScalarValue(_state.ToString()); |
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.
This will occur when _state
is a FormattedLogValues
object.
@@ -71,6 +83,10 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | |||
logEvent.AddPropertyIfAbsent(property); | |||
} | |||
} | |||
else | |||
{ | |||
scopeItem = propertyFactory.CreateProperty(NoName, _state).Value; |
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.
It's a shame we don't have CreatePropertyValue()
here, to skip the extra allocation. We could special-case string
to improve it a little.
Do you think, you may also inject values in Dictionary into the Scope? That would be awesome. I mean following line didn't appear in Scope which I believe it should using (_logger.BeginScope(new Dictionary<string, object> { ["ViaDictionary"] = 100 })) I know that the second parameter is object but still serialization to JSON would be 😎 |
Thanks for the feedback. I'm hesitant to add the extra weight to each log event that this would entail. The Adding a whole additional structured object to |
I hear you, and you are right. Thanks again! Is there a build that I can test? |
Feeling pretty good about the direction here - let's get it through to |
When
includeNamedScopes
is specified astrue
in theAddSerilog()
call, string-valued scope data will be included in a new property calledScope
on the resulting log events:No breaking changes, though a few constructors were added to
SerilogLoggerProvider
to facilitate this.Alternatives
The opt-in nature of the change is following the console logger provider, which requires
includeScopes
to be passed for scope information to be recorded.It could make sense to enable this by default, since developers are essentially opting in with
BeginScope()
, and object-valued (i.e. key-value-pair) scope state is already included.Thoughts?