获取NpgSql实体框架查询文本

时间:2019-02-16 11:35:23

标签: c# .net postgresql entity-framework-core npgsql

我正在使用Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0。 我希望能够看到将在数据库上执行的查询。 有什么办法吗?

1 个答案:

答案 0 :(得分:2)

如果在服务注册上使用LoggerFactory.AddDbContext,通常无需明确配置.AddDbContextPool。 EF Core会自动获取现有的记录器工厂(已配置),并使用“调试”级别日志来记录查询。

您也可以启用

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

您唯一需要做的就是相应地更改日志记录级别

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

或者您可以使用Custom logger工厂

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