简体   繁体   English

获取 NpgSql 实体框架查询文本

[英]Get NpgSql Entity Framework query text

I am using Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0.我正在使用 Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0。 I want to be able to see the query that will be executed on the database.我希望能够看到将在数据库上执行的查询。 Is there any way of doing this?有没有办法做到这一点?

Normally there is no need to configure LoggerFactory explcitly if you are using .AddDbContext or .AddDbContextPool on service registration.如果您在服务注册时使用.AddDbContext.AddDbContextPool ,通常不需要显式配置LoggerFactory EF Core automatically grabs existing logger factory (configured) and uses Debug level log in order to log queries. EF Core 自动获取现有记录器工厂(已配置)并使用调试级别日志来记录查询。

You can alternatively enable您也可以启用

builder.EnableSensitiveDataLogging();  // << Enables query parameter/value logging
builder.EnableDetailedErrors(); // << Enables Detailed Errors such as parameter mapping errors
builder.ConfigureWarnings(warnings => 
    warnings.Log(CoreEventId.IncludeIgnoredWarning));  << Includes ignored warnings

The only thing you need to do is to change the logging level accordingly您唯一需要做的就是相应地更改日志记录级别

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

or you may use Custom logger factory或者您可以使用自定义记录器工厂

public static readonly LoggerFactory MyLoggerFactory
    = new LoggerFactory(new[]
    {
        new ConsoleLoggerProvider((category, level)
            => category == DbLoggerCategory.Database.Command.Name
               && level == LogLevel.Information, true)
    });

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

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