In EF6 it was very simple to output the generated SQL with a one-line of code using Database.Logging property.
I tried to do everything discussed in this article as well as ASP.NET Core logging linked article, but it doesn't work.
Could you please provide a COMPLETE sample of how to see the EF generated SQL in a ASP.NET Core Web API?
Thanks.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Pl. check this article, may it help you...
It already writes every statement to the logs with loglevel Information.
Yes, for the love of god, please simply make it work like it did in EF 6. I thought you were going to do this a long time ago? I would swear this was on the list years ago. The ASP.NET Core logging infrastructure is an absolute train wreck. Not everyone is using this with ASP.NET Core. I had to jump through numerous hoops to get this working in an earlier version of EF Core and now that I have upgraded from EF Core 2.2 to 3.1, it no longer works and I see that the documentation on how to get it to work has changed. This should not be this complicated. Apparently, something on the life cycle of the logging provider changed or something. It works for me on the first instance of the DbContext. Then, when it goes out of scope, logging no longer works for additional instances. The way I've had it working up to this point was to add my provider using the following code in OnModelCreating().
this.GetService<ILoggerFactory>().AddProvider(new LoggingProvider());
I'm assuming it must have been storing it in a static variable behinds the scene in previous versions of EF and now it isn't.
This is implemented in EF Core 5! (Even better than in EF6)
See https://github.com/dotnet/efcore/issues/19549#issuecomment-572823246
Also document CreateDbCommand: https://github.com/dotnet/efcore/issues/19358
Might be worth updating this document to include a note that if you add 1 line to the LogLevel node appsettings.json that you will get EF Core logging:
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
Here is the full path to the setting:
{
"Logging": {
"LogLevel": {
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
}
}
}
Most helpful comment
Might be worth updating this document to include a note that if you add 1 line to the
LogLevelnode appsettings.json that you will get EF Core logging:"Microsoft.EntityFrameworkCore.Database.Command": "Information"Here is the full path to the setting: