-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
JsonConsoleFormatter to use array of scope to fix unique key issue #39860
Conversation
Tagging subscribers to this area: @maryamariyan |
var timestampFormat = FormatterOptions.TimestampFormat; | ||
if (timestampFormat != null) | ||
{ | ||
var dateTime = FormatterOptions.UseUtcTimestamp ? DateTime.UtcNow : DateTime.Now; | ||
timestamp = dateTime.ToString(timestampFormat); | ||
writer.WriteString("Timestamp", dateTime.ToString(timestampFormat)); |
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.
also would skip showing Timestamp when value is null
} | ||
else if (logEntry.State != null) | ||
{ | ||
writer.WriteString(nameof(logEntry.State), logEntry.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.
Also note the current implementation is missing the rendered message for some of the scopes, not sure if that should be tracked as separate issue?
(taken from #39664 (comment))
@alefranz I think this is what you were asking for.
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.
Not really, I was referring to the scope. Sorry I should have raised the issue before. Here it is: #39867
Regarding this change, in which scenarios would the state not be a k/v collection?
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.
Regarding this change, in which scenarios would the state not be a k/v collection?
Refer to test: Log_IncludeScopes_ContainsDuplicateNamedPropertiesInScope_AcceptableJson
using (logger.BeginScope("scope1 {name1}", 123))
using (logger.BeginScope("scope2 {name1} {name2}", 456, 789))
logger.Log(LogLevel.Information, 0, state: "exception message", exception: null, formatter: (a, b) => a);
@@ -79,10 +77,16 @@ public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeP | |||
|
|||
if (logEntry.State is IReadOnlyCollection<KeyValuePair<string, object>> stateDictionary) |
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.
IReadOnlyCollection [](start = 42, length = 19)
does it make sense to use IEnumerable rather than IReadOnlyCollection? this will give more coverage to other types.
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 is supposed to work if it is not IReadOnlyCollection
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.
So why we are testing the type? doesn't mean you can get other types?
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.
From my understanding the contract expects IReadOnlyCollection<KeyValuePair<string, object>> and that is why I check for it. (Referring to @davidfowl 's comment in #34742 (comment))
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 added a question. other than that LGTM.
I'm not super convinced as while it solve the duplicate keys issue, I had originally in mind mainly the At least for me, the main scenario to use scopes is to attach properties to have in all log entries (e.g. a transaction id) so it needs to be possible to query the datastore with this property no matter how nested is the specific property. For example: using (logger.BeginScope("{Number}", 2))
using (logger.BeginScope("{AnotherNumber}", 3))
{
logger.LogInformation("{LogEntryNumber}", 1);
} will become: {
"Timestamp": "2020-07-23T22:09:59.4750365\u002B01:00",
"EventId": 0,
"LogLevel": "Information",
"Category": "JsonLogging.Program",
"Message": "1",
"State": {
"LogEntryNumber": "1",
"{OriginalFormat}": "{LogEntryNumber}"
},
"Scopes": [
{
"Number": "2",
"{OriginalFormat}": "{Number}"
},
{
"AnotherNumber": "3",
"{OriginalFormat}": "{AnotherNumber}"
}
]
} I need to play with ingest this entries into a system like Seq or Kibana. |
CC @davidfowl |
I've tried in Seq and you can query with |
I added your use case as well to JsonConsoleFormatterTests UPDATE: using (logger.BeginScope("{Number}", 2))
using (logger.BeginScope("{AnotherNumber}", 3))
{
logger.LogInformation("{LogEntryNumber}", 1);
} we'd get:
|
57d5516
to
2e85a48
Compare
02f1367
to
bf1f03c
Compare
There is an issue to solve that I'll leave out of scope for this PR, and that is, we are now introducing more "Message" JSON elements per logged output. and it would be better if the name "Message" was reduced to something like There is a conversation about it here: #39664 (comment).
|
bf1f03c
to
54b74ed
Compare
fixed PR conflict through rebase |
Hello @maryamariyan! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Fixes duplicate keys in Json
Fixes: #39664