views:

4279

answers:

4

I'm using LINQ to Entities. I need to log the resulting SQL of every executed query. It'd be nice to log the execution times and the number of rows returned, though it's not crucial.

Is there a way to do this from within the app? I'd rather not use the SQL Profiler.

There should be a way to trace everything that goes from the framework to database. I can't modify every single usage of entity framework (so ToTraceString or some custom method call before iterating through results isn't an option) - it has to be a single solution working throghout the app.

+1  A: 

Duplicate question, see original here.

Edit: SQL Profiler and ToTraceString are the only options I know about.

David B
I don't think it's a duplicate question. I understand that Phobis calls his custom method every time he wants to trace SQL. I don't want to do that. I can't touch my existing code that uses queries. I want the entity framework to do all the tracing/logging automatically. Maybe it isn't possible?
the profiler is the least invasive solution here. anything else would require that you insert instrumentation (such as ToTraceString)
Joel Martinez
A: 

It won't be easy, but you could come up with a SQL trace listener for the .NET tracing system (there are several implementations out there) and then log every L2E query using ToTraceString and output the results to the SQL trace listener. That might work (it's a bit of work to get there, but it's definitely doable).

What you won't get here is any performance logging - this would be strictly logging the SQL queries as text.

Marc

marc_s
+2  A: 

This is closest to what you need - use a fake provider (sample included below) which will wrap existing one and log each query.

At that point enabling logging will be as straightforward as:

EFTracingProviderConfiguration.LogToConsole = true;
EFTracingProviderConfiguration.LogToFile = "MyLogFile.txt"

Provider sample on one of EF designers blog

deadbeef
A: 

It's not cheap, but EFProf is worth every penny.

mxmissile