-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Duplicate information in structured json logs #104409
Comments
@Scal-Human did you try to call |
Hello Tarek, |
@Scal-Human thanks for your response. Could you please provide a complete sample? Just if we suggest solution ensure it will work for your scenario. |
Sure, Tarek, dotnet new web Then added the json console logger to have the program.cs look like var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddJsonConsole();
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run(); When I dotnet run it gives Building...
{"EventId":14,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Now listening on: http://localhost:5173","State":{"Message":"Now listening on: http://localhost:5173","address":"http://localhost:5173","{OriginalFormat}":"Now listening on: {address}"}}
{"EventId":0,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Application started. Press Ctrl\u002BC to shut down.","State":{"Message":"Application started. Press Ctrl\u002BC to shut down.","{OriginalFormat}":"Application started. Press Ctrl\u002BC to shut down."}}
{"EventId":0,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Hosting environment: Development","State":{"Message":"Hosting environment: Development","EnvName":"Development","{OriginalFormat}":"Hosting environment: {EnvName}"}}
{"EventId":0,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Content root path: C:\\Study\\AspNetMinimal","State":{"Message":"Content root path: C:\\Study\\AspNetMinimal","ContentRoot":"C:\\Study\\AspNetMinimal","{OriginalFormat}":"Content root path: {ContentRoot}"}} Note that all messages outputed contain the message twice, plus the original format. {
"EventId": 14,
"LogLevel": "Information",
"Category": "Microsoft.Hosting.Lifetime",
"Message": "Now listening on: http://localhost:5173",
"State": {
"Message": "Now listening on: http://localhost:5173",
"address": "http://localhost:5173",
"{OriginalFormat}": "Now listening on: {address}"
}
} A dotnet --version gives 8.0.301 Thank you for your interest, |
Unfortunately, this is how Json console formatter work. It emits log entry state as a message (which just calls runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs Line 31 in 4071a31
runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs Line 61 in 4071a31
Then it emits the state object listing all state properties including the message again. runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs Line 71 in 4071a31
The workaround I can think of is you create the console logger and provide your custom formatter and you can format the entries the way you like. Let me know if you want to do that and need any help. I am closing the issue, feel free to send any more questions if you still have any. Thanks for your report! |
Thank you for those explanations. |
@Scal-Human To be honest, I don't know why this was originally done this way, and I don't have information on whether changing it would cause any issues. We might consider fixing it with a configuration switch or something similar, but this is not a high priority at the moment due to other pressing tasks we're currently handling. I'll reactivate the issue to track if we can do something about it in the future. |
Tarek, |
@Scal-Human have you considered just moving to a more modern approach to grabbing logs from applications, such as OpenTelemetry? I know this is not a direct answer to your problem, but I honestly believe you are moving in the wrong direction by approaching the problem the way you are currently. |
@julealgon thank you for your remark, but I wanted a reusable out-of-the-box solution to produce formatted trace and avoid parsing. |
I have this issue as well. Same steps to reproduce as above: #104409 (comment) example: {
"Timestamp": "2024-08-14 20:24:34Z",
"EventId": 101,
"LogLevel": "Information",
"Category": "System.Net.Http.HttpClient.ITwilioHttpClient.LogicalHandler",
"Message": "End processing HTTP request after 797.5402ms - 201",
"State": {
"Message": "End processing HTTP request after 797.5402ms - 201",
"ElapsedMilliseconds": 797.5402,
"StatusCode": 201,
"{OriginalFormat}": "End processing HTTP request after {ElapsedMilliseconds}ms - {StatusCode}"
}
} |
Any hope it gets fixed soon? Can do the PR if it helps.
Hmm think you missed some points about opentelemetry, it has two modes, one which is pushing the logs to the collector which is a huge regression since you can just loose the logs by design with the SDK implementation (and if you ensure it is not the case it is insanely inefficient) and the other is to pull the logs using something equivalent to promtail which needs a structure log format which is exactly what this issue is about, so yes this issue blocks to move to opentelemetry in good conditions until you implement a custom logger or formatter which makes default one not that consummable. Let me know if I should do the PR and on which branch to be able to consume it on next v9 release. |
.NET 9.0 has already been released, so we cannot modify its behavior. Addressing this might require introducing a new option in the JSON formatter or, at the very least, a configuration switch to control the different behaviors. However, adding new APIs to a released product is not feasible for .NET 9.0, and implementing a configuration switch in servicing releases is risky, as I prefer to have the default behavior be the correct one which avoids duplication. . This can be considered for .NET 10, we can discuss how we can address fixing the issue here. Thanks for your interest @rmannibucau |
@tarekgh while I get the high level policy, here we are speaking of something very located and trivial to fix. I agree changing the default would be better but I also ack it can't be done within a minor or .net 9.0 but awaiting 1 year to get a fix also means the solution is mostly not usable - the impact for a prod system is way too high - for one more year, so there must be something in between, should a MS.Logging.JsonFormatter be released and preview net10? |
Absolutely, if we address this issue in .NET 10, the fix will be included in the next preview release follow the fix. As a workaround, you can copy the current JSON provider code into your application, modify it as needed, and use it as a custom provider. I understand this isn't ideal, but it could help unblock you for now if necessary. |
Description
Using the json structured log with the intent to collect them from all containers without having to parse them, the message appears twice in full (bug) plus a third with the original format.
Either I missed the documentation or there is no way to configure what appears in the structure (with the built-in logger).
One could say I can writer a logger, but is it really the purpose of reinventing the wheel ... again ?
Reproduction Steps
Expected behavior
I am expecting, out-of-the-box, logging that contain usable information but without repetition.
Actual behavior
Here is an indented version of a single log line.
The message appears 2 times in plain text (this is a bug).
It appears a third time in the form of the original format which should be disableable in the options (JsonConsoleFormatterOptions).
Regression?
With x services times y nodes, it generates an unnecessary volume of logs leading back to a text-based log, custom logger or thirdparty solution while the information is already there.
Known Workarounds
As mentioned above, custom or thirdparty logger, but what is the framework for then ?
Configuration
I am using net8.0 on Windows 10/11 x64 (dev) and Unix 5.10 (prod).
Other information
I surfed a while now about the subject and did not found mention of this issue (duplicate message in single log).
If it is a duplicate, sorry for that and please point me to it.
The text was updated successfully, but these errors were encountered: