Aleksej_Shherbak
Aleksej_Shherbak

Reputation: 3058

How to switch on SQL queries logging with EF and Postgres?

I saw the following appsettings.json variant in the Adam Freeman's book:

{
  "ConnectionStrings": {
    "MyAppDb": "connection string"
  },
  "Logging": {
    "LogLevel": {
      "Default": "None",
      "Microsoft.EntityFrameworkCore": "Information"   
    }
  }
}

Then author tells:

This logging configuration will let you see the messages produced by Entity Framework Core that reveal the SQL commands that are sent to the database and prevent them from being lost in a stream of other messages.

I would like to see which SQL EF makes for my database. Unfortunately, the above approach does not work. Maybe it's happening because I'm using postgres and Npgsql and I have to put another setting in the appsettings.file? I have tried this:

{
  "ConnectionStrings": {
    "MyAppDb": "connection string"
  },
  "Logging": {
    "LogLevel": {
      "Default": "None",
      "Npgsql.EntityFrameworkCore.PostgreSQL": "Information"   
    }
  }
}

But without success( Just silence in the console. Pleas, help. Thank you.

Upvotes: 1

Views: 3524

Answers (1)

Nigel Maddocks
Nigel Maddocks

Reputation: 51

I'm using .NET Core 3.1, Npgsql 4.1.5, Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4, Npgsql.EntityFrameworkCore.PostgreSQL.Design 1.1.0.

SQL statements are logged out to Output window and the console (when running the Project).

appSettings.json (very similar to yours)


{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.EntityFrameworkCore": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "xxxxx"
  }

}


Example of output:

Microsoft.EntityFrameworkCore.Database.Command: Information: Executed DbCommand (1ms) [Parameters=[@__p_0='?' (DbType = Int64)], CommandType='Text', CommandTimeout='30']
SELECT p.id, p.country_id, p.dob, p.gender, p.handed, p.height_feet, p.height_inches, p.home_town, p.name, p.photo, p.turned_pro, p.weight
FROM tennis2.player AS p
WHERE p.id = @__p_0
LIMIT 1

So I would persevere.

Upvotes: 2

Related Questions