Skip to content

[BUG] .NET number values such as positive and negative infinity cannot be written as valid JSON. #189

@BenJury

Description

@BenJury

ECS integration/library project(s) (e.g. Elastic.CommonSchema.Serilog): Elastic.CommonSchema.NLog

ECS schema version (e.g. 1.4.0): 1.6.0-alpha1 (I think?)

ECS .NET assembly version (e.g. 1.4.2): 1.6.0-alpha1

Elasticsearch version (if applicable): 8.1.0

.NET framework / OS: net472

Description of the problem, including expected versus actual behavior: When we try to log an api call that restults in a NaN, we receive the following error in the nlog error file:

2022-07-15 08:43:26.6775 Error BufferingWrapper Target[api-log](File Target[apilogfile]): Error has been raised. Exception: System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON.
   at System.Text.Json.ThrowHelper.ThrowArgumentException_ValueNotSupported()
   at System.Text.Json.Utf8JsonWriter.WriteNumberValue(Double value)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
  ...snip...

There is an option to change the serialisers bahaviour with JsonSerializerOptions.NumberHandling, setting to JsonNumberHandling.AllowNamedFloatingPointLiterals works.

If anyone else is having this problem, it can be overridden using the code below:

            var type = typeof(Elastic.CommonSchema.Base).Assembly.GetType("Elastic.CommonSchema.Serialization.JsonConfiguration");
            var fieldInfo = type.GetField("SerializerOptions", BindingFlags.NonPublic | BindingFlags.Static);
            if (null != fieldInfo)
            {
                if (fieldInfo.GetValue(null) is JsonSerializerOptions options)
                {
                    options.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals;
                }
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions