Description
Consider the following code:
var serviceProvider = new ServiceCollection()
.AddLogging(options =>
{
options.AddJsonConsole(jsonOptions =>
{
jsonOptions.IncludeScopes = true;
jsonOptions.JsonWriterOptions = new JsonWriterOptions { Indented = true };
});
})
.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
using (logger.BeginScope(new Dictionary<string, object> { ["Id"] = "Value" }))
{
logger.LogInformation("Test");
}
You want to add an additional property by using the BeginScope method. It works but it also adds an unnecessary node Message for no reason:
{
"EventId": 0,
"LogLevel": "Information",
"Category": "Program",
"Message": "Test",
"State": {
"Message": "Test",
"{OriginalFormat}": "Test"
},
"Scopes": [
{
"Message": "System.Collections.Generic.Dictionary\u00602[System.String,System.Object]",
"Id": "Value"
}
]
}
It would be nice to either remove that Message node or add a flag to remove it:
https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs#L111
Reproduction Steps
You can test it here or by running the following code:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Text.Json;
public class Program
{
public static void Main()
{
var serviceProvider = new ServiceCollection()
.AddLogging(options =>
{
options.AddJsonConsole(jsonOptions =>
{
jsonOptions.IncludeScopes = true;
jsonOptions.JsonWriterOptions = new JsonWriterOptions { Indented = true };
});
})
.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
using (logger.BeginScope(new Dictionary<string, object> { ["Id"] = "Value" }))
{
logger.LogInformation("Test");
}
}
}
Expected behavior
It would be nice to have the following output:
{
"EventId": 0,
"LogLevel": "Information",
"Category": "Program",
"Message": "Test",
"State": {
"Message": "Test",
"{OriginalFormat}": "Test"
},
"Scopes": [
{
"Id": "Value"
}
]
}
Actual behavior
Currently, it prints the following result:
{
"EventId": 0,
"LogLevel": "Information",
"Category": "Program",
"Message": "Test",
"State": {
"Message": "Test",
"{OriginalFormat}": "Test"
},
"Scopes": [
{
"Message": "System.Collections.Generic.Dictionary\u00602[System.String,System.Object]",
"Id": "Value"
}
]
}
Regression?
No
Known Workarounds
No response
Configuration
No response
Other information
No response
Description
Consider the following code:
You want to add an additional property by using the
BeginScopemethod. It works but it also adds an unnecessary nodeMessagefor no reason:{ "EventId": 0, "LogLevel": "Information", "Category": "Program", "Message": "Test", "State": { "Message": "Test", "{OriginalFormat}": "Test" }, "Scopes": [ { "Message": "System.Collections.Generic.Dictionary\u00602[System.String,System.Object]", "Id": "Value" } ] }It would be nice to either remove that
Messagenode or add a flag to remove it:https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs#L111
Reproduction Steps
You can test it here or by running the following code:
Expected behavior
It would be nice to have the following output:
{ "EventId": 0, "LogLevel": "Information", "Category": "Program", "Message": "Test", "State": { "Message": "Test", "{OriginalFormat}": "Test" }, "Scopes": [ { "Id": "Value" } ] }Actual behavior
Currently, it prints the following result:
{ "EventId": 0, "LogLevel": "Information", "Category": "Program", "Message": "Test", "State": { "Message": "Test", "{OriginalFormat}": "Test" }, "Scopes": [ { "Message": "System.Collections.Generic.Dictionary\u00602[System.String,System.Object]", "Id": "Value" } ] }Regression?
No
Known Workarounds
No response
Configuration
No response
Other information
No response