Number of queries executed by EF4 per request
Is there a way to capture (and ultimately, log) the total number of queries that get executed by EF4, per request (or unit of work)?
I'm finding it easy for me to write some inefficient EF4 code due to it's easy access via lazy loading, and also due to poorly structured LINQ queries.
One easy way for me to be able to spot this wo开发者_开发百科uld be by capturing the total number of queries that are being executed, and log this with my request data, so that I can see which pages (controller/actions) are resulting in an excessive number of queries.
Is there a way to do this with EF4 code? I know I can do this with Profiler, and I'm already using this for now, but it would be much easier -- and more reliable -- if I could automate this.
Thanks in advance.
You will have to start with EF Tracing provider wrapper and use some logging infrastructure which will allow you to differing log records by request. You can either use thread Id, session Id or other identifier to differ log records For example .NET logging with TraceSource
supports CorrelationManager
an its StartLogicalOperation
and StopLogicalOperation
methods. I think you should be able to start logical operation when starting request processing and end logical operation when finishing request processing.
You may want to consider having a look at this: EF Profiler. [note: I am not related to this company in any way, just find it an awesome tool]
精彩评论