Entity Framework Core 3.1 DbCommandInterceptor only logging SELECT queries

624 views Asked by At

I have implemented a DbCommandInterceptor so that I can log the queries which EF is generating.

I have added the interceptor to my EF Context within the OnConfiguring method as follows

    optionsBuilder
        .UseSqlServer(dbConnectionString)
        .AddInterceptors(new DBContextInterceptorLogging(logger));

The interceptor works, in as much as I can see any SELECT sql query which is generated. However, I do not see any UPDATE, INSERT or DELETE queries being logged.

My DBContextInterceptorLogging class is starting a StopWatch in the overridden ScalarExecuting, NonQueryExecuting, ReaderExecuting, NonQueryExecutingAsync methods, and within the ScalarExecuted, NonQueryExecuted, ReaderExecuted, NonQueryExecutedAsync methods the StopWatch is stopped, and the DbCommand.CommandText is written out to the ILogger<>.

I have also overridden the CommandCreated, CommandFailed and CommandFailedAsync methods.

However, I never see a SQL INSERT/UPDATE/DELETE in my logger output.

If there some flag or filter I have inadvertently set?

1

There are 1 answers

0
PaulAdvancedUk On

I managed to find a solution to this via this article: https://christianfindlay.com/2020/07/19/sql-generated-by-entity-framework-core/

Rather than add an Interceptor the solution uses a LoggerFactory, with a custom ILogger implementation which extracts key information about the generated SQL.

From this I am able to write out the SQL, the parameters, and also see the time taken to execute the query.