Entityframework.docs: Simple way to see SQL statements

Created on 1 Mar 2019  Â·  6Comments  Â·  Source: dotnet/EntityFramework.Docs

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.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

area-diagnostics consider-for-next-release undocumented-feature

Most helpful comment

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"
    }
  }
}

All 6 comments

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)

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"
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidliang2008 picture davidliang2008  Â·  4Comments

norvegec picture norvegec  Â·  3Comments

jaxidian picture jaxidian  Â·  4Comments

ctaggart picture ctaggart  Â·  3Comments

SychevIgor picture SychevIgor  Â·  4Comments