Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log queries using Entity Framework 7?

I am using Entity Framework 7 on the nightly build channel (right now I'm using version EntityFramework.7.0.0-beta2-11524) and I'm trying to log the queries that EF generates just out of curiosity.

I'm writing a simple console program, I tried using the same logging technic that EF6 uses, but DbContext.Database.Logis not available on Entity Framework 7. Is there a way to log or just take a peek at the SQL generated by EF7?

like image 730
Mahmoud Ali Avatar asked Nov 05 '14 00:11

Mahmoud Ali


People also ask

How do I enable entity framework logging?

Simple logging feature can be enabled by just calling LogTo method while configuring a DbContext instance. This LogTo method expects an Action delegate which accepts a string as a parameter. LogTo method has provision to filter the log messages by allowing to specify log level in LogTo call.


1 Answers

For those using EF7 none of the above worked for me. But this is how i got it working. (from @avi cherry's comment)

In your Startup.cs you proably have a Configure method with a bunch of configurations in it. It should look like below (in addition to your stuff).

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        //this is the magic line
        loggerFactory.AddDebug(LogLevel.Debug); // formerly LogLevel.Verbose

        //your other stuff

    }
like image 67
BobbyTables Avatar answered Oct 05 '22 14:10

BobbyTables