|
| 1 | +--- |
| 2 | +title: "Breaking change: Avoid duplicate messages in Console logging with Json formatter" |
| 3 | +description: "Learn about the breaking change in .NET 10 where duplicate messages are avoided in Console logging with JSON formatter." |
| 4 | +ms.date: 01/15/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/dotnet/docs/issues/47006 |
| 7 | +--- |
| 8 | + |
| 9 | +# Avoid duplicate messages in Console logging with Json formatter |
| 10 | + |
| 11 | +When logging to the console using the JSON formatter, log messages are no longer duplicated in the log output. Previously, messages typically appeared three times: once as the top-level `Message`, again within the `State` object, and a third time as the original format string. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 Preview 7 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +Previously, when using a console logger configured with the JSON formatter, log messages were duplicated in the output. For example: |
| 20 | + |
| 21 | +```csharp |
| 22 | +logger.LogInformation("This is an information message."); |
| 23 | +``` |
| 24 | + |
| 25 | +This code would produce output like: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "EventId": 0, |
| 30 | + "LogLevel": "Information", |
| 31 | + "Category": "Program", |
| 32 | + "Message": "This is an information message.", |
| 33 | + "State": { |
| 34 | + "Message": "This is an information message.", |
| 35 | + "{OriginalFormat}": "This is an information message." |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +As you can see, `Message` appears twice—once as the top-level `Message` and again inside the `State` object. |
| 41 | + |
| 42 | +## New behavior |
| 43 | + |
| 44 | +After the change, the log output looks like this: |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "EventId": 0, |
| 49 | + "LogLevel": "Information", |
| 50 | + "Category": "Program", |
| 51 | + "Message": "This is an information message.", |
| 52 | + "State": { |
| 53 | + "{OriginalFormat}": "This is an information message." |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The `Message` is no longer duplicated—it now appears only at the top level, resulting in cleaner and more concise log output. |
| 59 | + |
| 60 | +## Type of breaking change |
| 61 | + |
| 62 | +This is a [behavioral change](../../categories.md#behavioral-change). |
| 63 | + |
| 64 | +## Reason for change |
| 65 | + |
| 66 | +The goal of this change is to reduce unnecessary logging overhead by eliminating duplicate content. By avoiding repeated formatting of the same message, the change helps: |
| 67 | + |
| 68 | +- **Minimize log output size** |
| 69 | +- **Reduce confusion caused by redundant information** |
| 70 | +- **Improve performance by preventing multiple formatting operations for the same message** |
| 71 | + |
| 72 | +Overall, this results in cleaner, more efficient, and easier-to-read logs. |
| 73 | + |
| 74 | +## Recommended action |
| 75 | + |
| 76 | +For users who were previously parsing the logging output and extracting the `Message` from within the `State` object, it's safe to use the top-level `Message` instead, now that duplication has been removed. |
| 77 | + |
| 78 | +> [!NOTE] |
| 79 | +> In some cases, a `Message` might still appear within the `State` object—this typically happens when its content differs from the top-level `Message`. |
| 80 | +
|
| 81 | +## Affected APIs |
| 82 | + |
| 83 | +- <xref:Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddConsole%2A?displayProperty=fullName> |
| 84 | +- [Microsoft.Extensions.Logging.Console NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console) |
0 commit comments