-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[Microsoft.Extensions.Logging] Bug: Logging arrays is broken and behaves unreliably #103338
Comments
Tagging subscribers to this area: @dotnet/area-extensions-logging |
You can also use the
|
Thank you for the suggestion. This NuGet package has indeed an effect on the logging methods created by the source generator. The ones without source generator nevertheless behave differently.
|
Closing this as |
I have to disagree. This is obviously a bug and not by design. Using This must be classified as a bug, not something that could be improved some day in the future. |
Fair enough. I reactivated the issue. |
This is a regression. To make the repro compatible with .NET SDK 6, I changed the initialization syntax to: private static readonly string[] Array1 = {"a", "b", "c"};
private static readonly string[] Array2 = {"d", "e", "f"}; In each test case below, the Microsoft.Extensions.Logging.Console package was the same version as Microsoft.Extensions.Logging.Abstractions. Microsoft.Extensions.Logging.Abstractions 6.0.0 on .NET SDK 6.0.424 targeting net6.0
This correctly formatted each array as a comma-delimited list. However, some arrays have brackets around them and others don't. That is because the generated LogGood method uses __LoggerMessageGenerator.Enumerate, which adds brackets, but the others use LogValuesFormatter, which doesn't. Microsoft.Extensions.Logging.Abstractions 7.0.0 on .NET SDK 6.0.424 targeting net6.0
The output is the same as with version 6.0.0 of the packages. Microsoft.Extensions.Logging.Abstractions 8.0.0 on .NET SDK 6.0.424 targeting net6.0
This is still the same. Microsoft.Extensions.Logging.Abstractions 7.0.0 on .NET SDK 8.0.303 targeting net8.0
This is still the same. Microsoft.Extensions.Logging.Abstractions 8.0.0 on .NET SDK 8.0.303 targeting net8.0
This shows the regression. Fixing it might be as simple as replacing |
Description
Depending on which option is used for logging (
Source Generators
,LoggerMessage.Define
,Logger.Log
) the resulting format for arrays (or any other type of enumeration) is different and sometimes even unusable.On the first glance the issue seems to be part of the
LogValuesFormatter
that tries to specially handle enumerations usingTryFormatArgumentIfNullOrEnumerable
.The issue is probably caused here where
TryFormatArgumentIfNullOrEnumerable
is not invoked for every parameter in some combinations:runtime/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogValuesFormatter.cs
Lines 207 to 215 in f6dc71b
Reproduction Steps
net8.0
Microsoft.Extensions.Logging.Console
NuGet in version8.0.0
Program.cs
Expected behavior
The resulting log should look something like this:
Actual behavior
The resulting log entries look different. Some options struggle with formatting arrays.
Regression?
No response
Known Workarounds
The issue can be minimized if the enumerations (e.g. an array) are passed as first parameters of the logging invocation. Alternatively the developer can manually format the arrays and pass the value as a string parameter.
Configuration
SDK version: 8.0.300
Other information
No response
The text was updated successfully, but these errors were encountered: