Skip to content

Consider using a format string for TimeSpan.TotalMilliseconds in log messages #14854

@Tragetaschen

Description

@Tragetaschen

Describe the bug

Since .NET Core 3, floating-point value's ToString behavior has changed. I now frequently see things like 200.19760000000002 ms in the log which looks rather clumsy.

To Reproduce

var ts = TimeSpan.FromTicks(2001976);
Console.WriteLine(FormattableString.Invariant($"{ts.TotalMilliseconds} ms"));
Console.WriteLine(FormattableString.Invariant($"{ts.TotalMilliseconds:F4} ms"));
200.19760000000002 ms
200.1976 ms

Expected behavior

TimeSpan.TicksPerMilliSecond == 10000, so a format string of F4 shows all the relevant digits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsclean-upThis issue is internal clean-up and has no effect on public APIs or expected behaviorsfeature-platformDeprecated: Cross-cutting issues related to ASP.NET Core as a platform

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions