denver
denver

Reputation: 3135

ASP.NET Core 2.2 Entity Framework Logging

In the past I have seen clear printouts of the SQL statements from Entity Framework sent to the logger, historically by setting DbContext.Database.Log. Things seem to have changed and I can't get them to work.

This page by MS implies that configuring logging for ASP.NET Core automatically configures entity logging. Following this I have configured the logging using the appsettings.json file as below:

{
   "Logging": {
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information",
      "Microsoft.EntityFrameworkCore": "Debug",
    }
  }
}

This does result in lots of logs from the Entity Framework Core showing up, however none of them are the queries. I would expect to see entries from Microsoft.EntityFrameworkCore.Database.Command but I do not. How do I get the SQL statements in the logs?

I know this looks similar to a few other questions, but none of them helped me out.

Update: All the responses were on the right track. The actual issue was the provider in use. For preliminary development we were using the in memory database. This provider does not log SQL statements (it isn't an SQL database). For completeness the logger configuration needed to be:

{
  "Logging": {
    "LogLevel": {
      "Microsoft.EntityFrameworkCore.Database.Command": "Information"
    }
  }
}

Upvotes: 2

Views: 3643

Answers (1)

Bryan Dellinger
Bryan Dellinger

Reputation: 5294

this is the one I stole out of Adam Freeman's Pro Entity Framework Core 2 book and I see the sql statements

https://github.com/Apress/pro-ef-core-2-for-asp.net-core-mvc/blob/master/03%20-%20Working%20with%20Databases/PartyInvites/PartyInvites/appsettings.json

{
"Logging":{
     "LogLevel": {
          "Default": "None",
          "Microsoft.EntityFrameworkCore": "Information"
          }
        }
}

Upvotes: 1

Related Questions