How to capture all SQL sent over Ado.Net

ado.net c# dapper entity-framework logging

Question

I have an application that uses both Entity Framework and Dapper. I would like to provide a custom logger to log out any sql that is issued over the ado.net connection. What is the best way of doing this?

Alternately, if it's not easily possible what exactly are the difficulties?

Accepted Answer

Would ADO.NET Data Tracing work for you? It plugs into ETW, from which you might be able to glue your current logging system. Doesn't seem much stand-alone though.


Expert Answer

Miniprofiler, which is written by the same team, works with both dapper and Entity Framework - although iirc the later requires a bit more work. This provides the ability for both logging and profiling of ADO.NET commands. We use it 24x7 here on stackoverflow (and the entire stack exchange network), so it works well even at scale.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why