When using an O/RM, poor performing SQL statements are often not discovered until you (or your DBA) find that a particular query is slowing down your database server. At this point, it becomes hard to identify which piece of application code is causing that SQL to be executed.
An interceptor to log slow/failed SQL
In EF6.0.0 we introduced interceptors, which allow you to get into the pipeline just before, and just after, a query/command is sent to the database.
Here is an interceptor that detects queries/commands that either failed, or exceeded a chosen threshold for execution time. In addition to the query being executed, it logs the call stack. This allows us to identify which piece of our application code initiated the database operation.
This example writes to a log file, but you can log the information to wherever needed.
Registering the interceptor
The easiest way to get our…
View original post 133 more words