简体   繁体   English

如何使用 EF 和 Postgres 打开 SQL 查询日志记录?

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

I saw the following appsettings.json variant in the Adam Freeman's book:我在 Adam Freeman 的书中看到了以下appsettings.json变体:

{
  "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.此日志记录配置将让您查看 Entity Framework Core 生成的消息,这些消息显示发送到数据库的 SQL 命令,并防止它们在 stream 的其他消息中丢失。

I would like to see which SQL EF makes for my database.我想看看哪个 SQL EF为我的数据库制作。 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 ?也许它正在发生,因为我正在使用postgresNpgsql并且我必须在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.但没有成功(只是在控制台中保持沉默。请帮助。谢谢。

I'm using .NET Core 3.1, Npgsql 4.1.5, Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4, Npgsql.EntityFrameworkCore.PostgreSQL.Design 1.1.0.我正在使用 .NET Core 3.1、Npgsql 4.1.5、Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4、Npgsql.EntityFrameworkCore.Z399BD1EE587245ECAC6F39BEAA.FZ90.6

SQL statements are logged out to Output window and the console (when running the Project). SQL 语句注销到 Output window 和控制台(运行项目时)。

appSettings.json (very similar to yours) appSettings.json(与您的非常相似)


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

}


Example of output: 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.所以我会坚持下去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM