79619286

Date: 2025-05-13 08:50:00
Score: 0.5
Natty:
Report link

@canton7's response answers the original question.

Dapper doesn't have interceptors, so to solve your real problem (add logging) you have two options:

1. Make own extension methods (bad option):

Make methods like .LoggingQueryAsync(...).
It looks simple at first, but have way too many downsides...

2. Implement IDbConnection method that Dapper calls (good option):

public class LoggingDbConnection : IDbConnection
{
    ...

    public IDbCommand CreateCommand()
    {
        return new LoggingDbCommand(this);
    }

    ...
}

Dapper have to call IDbConnection.CreateCommand() to do anything.

In LoggingDbCommand implement IDbCommand.ExecuteNonQuery(), IDbCommand.ExecuteReader(), IDbCommand.ExecuteReader(CommandBehavior) and IDbCommand.ExecuteScalar() to add logging.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @canton7's
  • Low reputation (0.5):
Posted by: Sinus32