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.Log
is not available on Entity Framework 7. Is there a way to log or just take a peek at the SQL generated by EF7?
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With