-
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
ILogger throws System.FormatException in .net8 but the same code works in .net7 #97852
Comments
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsDescriptionAfter upgrading our codebase to .NET8, we noticed a lot of System.FormatExceptions coming from the ILogger interface when we try to log some messages. Below is a minimal example that works in .NET7 but crashes in .NET8. Reproduction Stepsusing Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddSerilog(dispose: true);
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Hello, World!");
logger.LogInformation("Hello, {Name}!", "Serilog");
// Crashes in .NET8 but works with .NET7
logger.LogInformation("Hello, {Name}}!", "Serilog");
Console.ReadLine(); Expected behavior[07:27:26 INF] Hello, World! Actual behavior[07:21:01 INF] Hello, World! ..\bin\Debug\net8.0\ILoggerCrashExample.exe (process 25560) exited with code -1. Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Isn't the old behaviour incorrect? To get the resulting string The string |
So what would be a good approach here to avoid the problem? Edit: |
Then it's choosing to be more lenient on something that is erroneous.
Why is the format string being dynamically generated? Best practice is to not do that: have a fixed set of placeholders in a well-defined format string, and put any dynamic content into the placeholders as arguments. |
The problem was that the object was only more or less JSON stringified and sent to the logging library. |
This looks you need to some validation before sending the string to the logger library. Not doing that can end with undesired or surprised behavior. Even with Serilog, it is kind of hiding the problem. Also, if you have control on the object get serialized, maybe good to fix that there. |
This issue has been marked |
I have already adjusted the serialization of the object so that this no longer happens. So the solution would be that when I log an object in serialized form, it first goes through a validation before it is logged. |
I am closing the issue but feel free to respond if you still have any questions. |
Description
After upgrading our codebase to .NET8, we noticed a lot of System.FormatExceptions coming from the ILogger interface when we try to log some messages.
This only happens with some types of messages. After a closer look, messages created during runtime like these cause the problem.
"Hello, {name}}!", "Serilog"
It seems that something has changed within the message parser when log messages are provided with arguments.
In .NET7 it worked.
Below is a minimal example that works in .NET7 but crashes in .NET8.
Reproduction Steps
Expected behavior
[07:27:26 INF] Hello, World!
[07:27:26 INF] Hello, Serilog!
[07:27:26 INF] Hello, {Name}}!
Actual behavior
[07:21:01 INF] Hello, World!
[07:21:02 INF] Hello, Serilog!
..\bin\Debug\net8.0\ILoggerCrashExample.exe (process 25560) exited with code -1.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: