Skip to content

Improve parameter logging #5342

@balazsmeszegeto

Description

@balazsmeszegeto

Steps to reproduce

When parameter logging is enabled, only "simple" parameters' values are actually shown in the logs. Types such as arrays will appear for example as System.Int32[]

Example code:

await using var cmd = dataSource.CreateCommand("UPDATE employee SET responsibility = 'Accounting' WHERE id = ANY($1)");
cmd.Parameters.Add(new NpgsqlParameter<int[]>() { TypedValue = ids, NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Integer });
await cmd.ExecuteNonQueryAsync();

The issue

The issue is that command parameters are passed as object[] and log message (for completed execution) is: "Command execution completed (duration={DurationMs}ms): {CommandText}\n Parameters: {Parameters}"

Microsoft.Extensions.Logging unwraps "one level" of IEnumerable values (LogValuesFormatter from Microsoft.Extensions.Logging.Abstractions assembly), but won't do this recursively, so only "simple" types' values will appear in the logs.

Further technical details

Npgsql version: 6+

Suggestion

I see that NpgSqlCommand already "prepares" the parameter list using a local method ParametersDbNullAsString inside LogExecutingCompleted, but that only translates DBNull values to NULL string literal, all other types are untouched.

This local method could be further enhanced, for example recognize IEnumerable.

I'd be happy to contribute 😃 If my contribution is welcome, then please let me know if my proposal enhancing the referred local method is a way to go

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions