dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.78k stars 3.19k forks source link

change LogExecutedCommand elapsed parameter from string to int or do not send thousands seperator #34281

Closed dovjar closed 1 month ago

dovjar commented 3 months ago

current efcore logging for executed command with message template: Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}

logs elapsed metric as string. code from RelationalCommandDiagnosticsLogger.cs method CommandReaderExecuted code

definition.Log(
                this,
                string.Format(CultureInfo.InvariantCulture, "{0:N0}", duration.TotalMilliseconds),
                command.Parameters.FormatParameters(ShouldLogParameterValues(command)),
                command.CommandType,
                command.CommandTimeout,
                Environment.NewLine,
                command.CommandText.TrimEnd());

duration.TotalMilliseconds is formated using InvariantCulture with 0 decimal places. But still writes comma as thousand seperator. e.g. string.Format(CultureInfo.InvariantCulture, "{0:N0}",1234,5m) // will return 1,234 Therefore log tools will not recognize this value as number and you will not be able to use these metric as number. for example can't do aggregations.

change property to int as in the same class method: DataReaderDisposing

if (ShouldLog(definition))
{
    _suppressDataReaderDisposingExpiration = default;
    definition.Log(this, connection.DbConnection.Database, connection.DbConnection.DataSource, (int)duration.TotalMilliseconds);
}
ajcvickers commented 3 months ago

Related to #33598 (likely dupe.)

roji commented 1 month ago

Duplicate of #3598