ASP.NET Core 2.2 Entity Framework Logging

asp.net-mvc c# entity-framework-core

Question

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"
    }
  }
}
1
0
10/11/2019 4:55:38 PM

Accepted Answer

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"
          }
        }
}
0
10/8/2019 6:58:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow