runtime: Duplicate information in structured json logs
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
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddJsonConsole((JsonConsoleFormatterOptions options) => {
options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffffffzzz";
options.JsonWriterOptions = new JsonWriterOptions {
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping // Because + is a dangerous character
};
});
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.
{
"Timestamp": "2024-07-04T01:11:36.9360021+02:00",
"EventId": 2,
"LogLevel": "Information",
"Category": "Microsoft.AspNetCore.Hosting.Diagnostics",
"Message": "Request finished HTTP/1.1 GET http://venium:8076/api/really/anything - 200 54 text/plain;charset=utf-8 17.1633ms",
"State": {
"Message": "Request finished HTTP/1.1 GET http://venium:8076/api/really/anything - 200 54 text/plain;charset=utf-8 17.1633ms",
"ElapsedMilliseconds": 17.1633,
"StatusCode": 200,
"ContentType": "text/plain; charset=utf-8",
"ContentLength": 54,
"Protocol": "HTTP/1.1",
"Method": "GET",
"Scheme": "http",
"Host": "venium:8076",
"PathBase": "",
"Path": "/api/really/anything",
"QueryString": "",
"{OriginalFormat}": "Request finished {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} - {StatusCode} {ContentLength} {ContentType} {ElapsedMilliseconds}ms"
}
}
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.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 25 (12 by maintainers)
@rmannibucau the case of
logger.LogInformation("{Message}", "foo");is a special one, simply because the placeholder’s name coincides with the nameMessage. Let’s first align on the core formatting behavior:logger.LogInformation("{P1}", "foo");should result in"P1": "foo"in the structured state.logger.LogInformation("{P1}{P2}", "foo", "bar");should produce"P1": "foo", "P2": "bar"in the state.So, when the placeholder is named
Message, it logically follows that"Message": "foo"would appear in the state as well—even if the main log message itself is"foo". Therefore, in the case oflogger.LogInformation("{Message}", "foo");, you’ll see"Message": "foo"both in the top-level message and in the structured state.After my fix, you’ll no longer see duplicate
"Message"entries in the state. The result would look like:And for something like
logger.LogInformation("{Message}.", "foo");, the output would be:In summary, the behavior after the fix is both consistent and correct with respect to how structured logging is intended to work.
Sure, Tarek, I simply created a web
Then added the json console logger to have the program.cs look like
When I dotnet run it gives
Note that all messages outputed contain the message twice, plus the original format. The first line in indented format gives:
A dotnet --version gives
Thank you for your interest, Scal
No, I didn’t modify this part, but it seems like a minor issue. I expect few, if any, users to encounter it. The workaround is simple: avoid using placeholder names like
OriginalFormat.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.
@julealgon thank you for your remark, but I wanted a reusable out-of-the-box solution to produce formatted trace and avoid parsing. With a solution in the framework itself, it prevents having X ways to do the same thing, avoid dependencies on thridparty tools, and improve the quality of information. At first glance, the Json formatter was exactly what I wanted, at the price of 2 or 3 lines of a configuration that can be reused from service to service, but I really have a problem with the duplication of the messages. The way the logging have been implemented allows not only message identification (category, eventId), but preserves the actual information the message is related to in separate state fields. This is great as it allows not only humans to analyze a particular event, but allows automated analyzis with data extraction without (possibly faulty) parsing. Yet, I will definitely have a deeper look at OpenTelemetry, thank you.
Tarek, Thank you for re-activating it (you have no idea how much it touches me 😉 but I agree (and understand) this is not a priority. To be honest too, I think that it may cause problems to persons that are already using this format to collect logs. Some may rely on the Message field, while some others may rely on the State.Message field. So anyhow, solving this kind of bug causes a breaking change. In the meantime, I wrote a formatter of my own … the Nth one.