JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log call
Maintainer thường phản hồi trong vòng 1 ngày
@Mohamedadel-prog đang làm issue này rồi.
Từ ngày 28/11/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- csharp, json
- Lĩnh vực
- backend, observability
Hướng nghiên cứu
Bắt đầu tại JsonConsoleFormatter.WriteItem, nơi issue chỉ ra các lệnh gọi Utf8JsonWriter.WriteNumber vô điều kiện với double và float. Kiểm tra các bài kiểm thử của formatter đối với state, scopes và thuộc tính của bản ghi được đệm, sau đó chạy các bài kiểm thử liên quan. Hoàn tất khi NaN và vô cực dương, vô cực âm được ghi dưới dạng chuỗi bất biến và các lệnh gọi ghi log hoàn thành mà không gây ngoại lệ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
JsonConsoleFormatter writes a double or float value of an entry's state, of a scope, or of a buffered record's attributes with Utf8JsonWriter.WriteNumber, which refuses NaN, positive infinity and negative infinity. The ArgumentException leaves the formatter, so the log call itself throws an AggregateException and no line is written. With IncludeScopes, a scope holding such a value makes every log call inside it throw. The same calls through the simple formatter write their messages.
A log call should not fail because of a value's content: on a service that logs rates or prices, a NaN is an ordinary value to log.
Reproduction Steps
// Microsoft.Extensions.Logging.Console 11.0.0
using Microsoft.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(builder => builder.AddJsonConsole(options => options.IncludeScopes = true));
var logger = loggerFactory.CreateLogger("Repro");
logger.LogInformation("Rate {Rate}", double.NaN); // throws
using (logger.BeginScope("Rate {Rate}", double.PositiveInfinity))
{
logger.LogInformation("Inside a scope"); // throws
}
Expected behavior
The entry is written. NaN and the infinities could be written as the strings "NaN", "Infinity" and "-Infinity", as System.Text.Json writes them under JsonNumberHandling.AllowNamedFloatingPointLiterals, which the exception text itself suggests, the way the formatter already writes a value of any type it has no case for as a string.
Actual behavior
System.AggregateException: An error occurred while writing to logger(s). (.NET number values such as positive and negative infinity cannot be written as valid JSON. To make it work when using 'JsonSerializer', consider specifying 'JsonNumberHandling.AllowNamedFloatingPointLiterals' (see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).)
---> System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON. To make it work when using 'JsonSerializer', consider specifying 'JsonNumberHandling.AllowNamedFloatingPointLiterals' (see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).
at System.Text.Json.ThrowHelper.ThrowArgumentException_ValueNotSupported()
at System.Text.Json.Utf8JsonWriter.WriteNumber(ReadOnlySpan`1 propertyName, Double value)
at System.Text.Json.Utf8JsonWriter.WriteNumber(String propertyName, Double value)
at Microsoft.Extensions.Logging.Console.JsonConsoleFormatter.WriteItem(Utf8JsonWriter writer, KeyValuePair`2 item)
at Microsoft.Extensions.Logging.Console.JsonConsoleFormatter.WriteInternal(IExternalScopeProvider scopeProvider, TextWriter textWriter, String message, LogLevel logLevel, String category, Int32 eventId, String exception, Boolean hasState, String stateMessage, IReadOnlyList`1 stateProperties, DateTimeOffset stamp)
at Microsoft.Extensions.Logging.Console.JsonConsoleFormatter.Write[TState](LogEntry`1& logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter)
at Microsoft.Extensions.Logging.Console.ConsoleLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
--- End of inner exception stack trace ---
at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions)
JsonWriterOptions.SkipValidation = true does not change it, and a float throws the same way.
Regression?
No: Microsoft.Extensions.Logging.Console 10.0.0 throws the same way.
Known Workarounds
A ConsoleFormatter of one's own that presents the state, the scopes and a buffered record's attributes with such values as strings, then hands the entry to the JSON formatter.
Configuration
.NET 11.0 (SDK 11.0.100), Microsoft.Extensions.Logging.Console 11.0.0 and 10.0.0, Windows 11 x64. WriteItem has no platform-specific code.
Other information
WriteItem has a case double and a case float that call writer.WriteNumber unconditionally; a check with double.IsFinite / float.IsFinite there, writing the invariant string otherwise, would fix it for state, scopes and buffered records at once.
- Ngôn ngữ chính
- C#
- Star
- 18.5k
- Fork
- 5.8k
- Merge trung bình
- 2 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 597
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/runtime
-
area-VM-meta-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area-Debugger-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
dotnet/runtime#138494 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-AssemblyLoader-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#138493 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-System.IO untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#138417 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-Codegen-AOT-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của dotnet/runtime
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
microsoft/sqltoolsservice#2851 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
elsa-workflows/elsa-foundation#6322 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
mohebifar/made-in-iran#273 ·
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with TakeCó thể đã có người làm @Mohamedadel-prog đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
dotnet/efcore#39848 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày