JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log call
维护者通常 1 天内回复
@Mohamedadel-prog 已经在做这个了。
开始于 2026年11月28日。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 82/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp, json
- 领域
- backend, observability
调研方向
从 JsonConsoleFormatter.WriteItem 开始,issue 指出其中无条件调用 Utf8JsonWriter.WriteNumber 处理 double 和 float。检查 formatter 测试中的 state、scopes 和缓冲记录属性,然后运行相关测试。完成标准是 NaN、正无穷和负无穷均写为不变字符串,并且日志调用完成时不会抛出异常。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- C#
- 星标
- 18.5k
- 派生
- 5.8k
- 平均合并
- 2 天 8 小时
- 30 天内合并 PR
- 597
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/runtime 的其他 Issue
-
area-VM-meta-mono untriaged
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
area-Debugger-mono untriaged
难度 2/5 1-3 小时 新手友好度 75/100
dotnet/runtime#138494 · 1 条评论 ·
维护者通常 1 天内回复
-
area-AssemblyLoader-mono untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#138493 · 1 条评论 ·
维护者通常 1 天内回复
-
area-System.IO untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#138417 · 1 条评论 ·
维护者通常 1 天内回复
-
area-Codegen-AOT-mono untriaged
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
microsoft/sqltoolsservice#2851 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
needs-triage
难度 2/5 1-3 小时 新手友好度 83/100
elsa-workflows/elsa-foundation#6322 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 74/100
mohebifar/made-in-iran#273 ·
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with Take可能已有人在做 @Mohamedadel-prog 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 2 天内回复