Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture all SQL sent over Ado.Net

Tags:

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?

like image 807
George Mauer Avatar asked Oct 19 '13 19:10

George Mauer


People also ask

How do I fetch all records in SQL?

To retrieve all columns, use the wild card * (an asterisk). The FROM clause specifies one or more tables to be queried. Use a comma and space between table names when specifying multiple tables. The WHERE clause selects only the rows in which the specified column contains the specified value.

How can I see SQL generated by LINQ?

You can view the SQL code generated for queries and change processing by using the Log property. This approach can be useful for understanding LINQ to SQL functionality and for debugging specific problems.

How do I get data from ExecuteReader?

To retrieve data using a DataReader, create an instance of the Command object, and then create a DataReader by calling Command. ExecuteReader to retrieve rows from a data source.


2 Answers

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.

like image 69
tne Avatar answered Oct 03 '22 05:10

tne


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.

like image 41
Marc Gravell Avatar answered Oct 03 '22 05:10

Marc Gravell