4

Is there a simple way to log all linq queries executed in an application?

I am currently using code first entity framework with a DbContext instead of the older DataBaseContext.I know for a fact that the DataBaseContext class has a Log property, but i do not know if there is something like the log property for the DbContext.

mipe34
  • 5,596
  • 3
  • 26
  • 38
Tom
  • 139
  • 1
  • 14
  • Possible duplicate: http://stackoverflow.com/questions/6550046/using-mvc-mini-profiler-database-profiling-with-entity-framework-code-first – Dennis Jan 17 '13 at 09:21
  • 1
    No [EF unfortunately doesn't have any build in logging](http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1051017-support-for-sql-logging). You must use SQL profiler, EF related profiler or you must write EF provider wrapper which will do that. – Ladislav Mrnka Jan 17 '13 at 09:48
  • Possible duplicate: http://stackoverflow.com/questions/174261/entity-framework-and-queries-sql-logging – mipe34 Jan 17 '13 at 09:49

1 Answers1

1

You can use .ToString() on IQueryable queries that you'll create. It will return SQL statement that will be executed on DB.
Then if you're using Repository pattern you can implement your logging logic without using any third-party libs.

Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54