4

I need to do a log of all SQL statements done. I'm using EF5 and this is my context constructor

public partial class Entities : ObjectContext
{
    public const string ConnectionString = "name=Entities";
    public const string ContainerName = "Entities";

    #region Constructors

    public Entities()
        : base(ConnectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }

Update:

This works fine for me: http://efwrappers.codeplex.com/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Douglas Franco
  • 591
  • 5
  • 13
  • 1
    possible duplicate of [Logging SQL statements of Entity Framework 5 for database-first aproach](http://stackoverflow.com/questions/15829898/logging-sql-statements-of-entity-framework-5-for-database-first-aproach) – Michael Edenfield Jan 14 '15 at 21:14
  • tkz, i'll see, i think its a perfect solution for my case – Douglas Franco Jan 14 '15 at 21:16
  • It looks like the codeplex link is broken... My guess it that the same content can be found at https://ripcode.net/p/efwrappers (... even if I wasn't able to make it work with EF5 and ObjectContext) – NiceGuyAlberto Dec 10 '21 at 10:36

2 Answers2

3

something like this:

context.Database.Log = Console.Write; 

for more info:

http://msdn.microsoft.com/en-us/data/dn469464.aspx

Z .
  • 12,657
  • 1
  • 31
  • 56
0

For those coming here at this point in time. You can now easily log EF queries by configuring an interceptor in your configuration:

<interceptors>
  <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
    <parameters>
      <parameter value="C:\Temp\LogOutput.txt"/>
    </parameters>
  </interceptor>
</interceptors>

https://learn.microsoft.com/en-us/ef/ef6/fundamentals/configuring/config-file

Dave de Jong
  • 859
  • 7
  • 13