-
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
Formatting a log message: repeating the same positional parameter leads to exception #50036
Comments
Tagging subscribers to this area: @maryamariyan Issue DetailsDescription of the bugWhen issuing a log message trough ILogger, where the format string repeats a positional parameter, an exception is raised by a formatting layer of the logging extensions:
To ReproducePlease refer to the attached VS 2019 solution. Expected behaviorThe above programs logs "FooFooBar" to the console. Screenshots resp. exception stack traceUnhandled exception. System.AggregateException: An error occurred while writing to logger(s). (Index (zero based) must be greater than or equal to zero and less than the size of the argument list.) C:\projects\LoggingFormatError\bin\Debug\net5.0\LoggingFormatError.exe (process 12888) exited with code -532462766. Additional contextThe behavior is the same on .NET 4.7.2, .NET Core 3.1 and .NET 5.0. The attached solution is built on .NET 5.0
|
AFAIK, Microsoft.Extensions.Logging is not designed to support positional parameters at all. See Log message template. The analysers in #36064 would give warning MEL0001 for this code. |
You are right. When I reported the exception that started the thread, I was under the belief that the string fed with numbers as placeholders behaves like "traditional format string". It is not, as you write: it is a message template made to support structured logging. It is a pity that the "official" ILogger documentation (e.g. https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerextensions.logerror?view=dotnet-plat-ext-5.0#Microsoft_Extensions_Logging_LoggerExtensions_LogError_Microsoft_Extensions_Logging_ILogger_Microsoft_Extensions_Logging_EventId_System_Exception_System_String_System_Object___ ) is not very explicit about this: it does not explain what "message template format" is in contrast to a format string. Since my usage is wrong, and since the recommended log analyzer gives a warning for the wrong usage, we can close the issue. |
A few hyperlinks from the reference documentation to the conceptual documentation would make the correct usage easier to understand. I don't think there is an open issue for adding any, although dotnet/dotnet-api-docs#5039 looks like it would cover adding such a link in the Microsoft.Extensions.Logging Namespace topic, and #48951 might make such links easier to add in method parameter documentation. |
Description of the bug
When issuing a log message trough ILogger, where the format string repeats a positional parameter, an exception is raised by a formatting layer of the logging extensions:
To Reproduce
Please refer to the attached VS 2019 solution.
LoggingFormatError.zip
Expected behavior
The above programs logs "FooFooBar" to the console.
Screenshots resp. exception stack trace
Unhandled exception. System.AggregateException: An error occurred while writing to logger(s). (Index (zero based) must be greater than or equal to zero and less than the size of the argument list.)
Additional context
The behavior is the same on .NET 4.7.2, .NET Core 3.1 and .NET 5.0. The attached solution is built on .NET 5.0
The text was updated successfully, but these errors were encountered: