2

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.

2
  • 1
    You can set log_statement = all on the pg server side. See doc Commented Nov 11, 2019 at 8:58
  • @clamp thank you, but I would like to see exactly this feature. I mean debug console of a running .NET Core application. It seems that it works for SQL Server. I hope it works for PostgreSQL too. Commented Nov 11, 2019 at 9:04

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.