Dapper 和 Postgres - 打印查询
Dapper and Postgres - print queries
我正在将 Dapper 与 Postgres 一起使用,我想记录一些诊断信息,尤其是实际查询 运行。
如果我使用 SQL 服务器,我会做
let connection = new SqlConnection(connectionString)
connection.StatisticsEnabled = true
// run query
let stats = connection.RetrieveStatistics()
但我似乎找不到 NpgsqlConnection
的类似内容。
检索 运行 for NpgsqlConnection
的实际查询的最佳方法是什么?
我能够通过创建 NpgSql 日志记录提供程序来打印查询,如 here 所述。
type SqlLogger (logger: ILogger) =
inherit NpgsqlLogger() with
let mapLogLevel level =
match level with
| NpgsqlLogLevel.Trace -> LogLevel.Debug
| NpgsqlLogLevel.Debug -> LogLevel.Debug
| NpgsqlLogLevel.Info -> LogLevel.Information
| NpgsqlLogLevel.Warn -> LogLevel.Warning
| NpgsqlLogLevel.Error -> LogLevel.Error
| NpgsqlLogLevel.Fatal -> LogLevel.Critical
| _ -> LogLevel.Debug
override __.IsEnabled(_level : NpgsqlLogLevel) = true
override __.Log(level, connectorId, msg, ex) =
let level = mapLogLevel level
logger.Log(level, ex, msg)
type SqlLoggerProvider (loggerFactory: ILoggerFactory ) =
member __.loggerFactory = loggerFactory
interface INpgsqlLoggingProvider with
member __.CreateLogger(name) = loggerFactory.CreateLogger(name) |> SqlLogger :> NpgsqlLogger
然后像这样注册它:
let configureApp (app : IApplicationBuilder) =
NpgsqlLogManager.Provider <- SqlLoggerProvider (app.ApplicationServices.GetService<ILoggerFactory>())
NpgsqlLogManager.IsParameterLoggingEnabled <- true
我正在将 Dapper 与 Postgres 一起使用,我想记录一些诊断信息,尤其是实际查询 运行。
如果我使用 SQL 服务器,我会做
let connection = new SqlConnection(connectionString)
connection.StatisticsEnabled = true
// run query
let stats = connection.RetrieveStatistics()
但我似乎找不到 NpgsqlConnection
的类似内容。
检索 运行 for NpgsqlConnection
的实际查询的最佳方法是什么?
我能够通过创建 NpgSql 日志记录提供程序来打印查询,如 here 所述。
type SqlLogger (logger: ILogger) =
inherit NpgsqlLogger() with
let mapLogLevel level =
match level with
| NpgsqlLogLevel.Trace -> LogLevel.Debug
| NpgsqlLogLevel.Debug -> LogLevel.Debug
| NpgsqlLogLevel.Info -> LogLevel.Information
| NpgsqlLogLevel.Warn -> LogLevel.Warning
| NpgsqlLogLevel.Error -> LogLevel.Error
| NpgsqlLogLevel.Fatal -> LogLevel.Critical
| _ -> LogLevel.Debug
override __.IsEnabled(_level : NpgsqlLogLevel) = true
override __.Log(level, connectorId, msg, ex) =
let level = mapLogLevel level
logger.Log(level, ex, msg)
type SqlLoggerProvider (loggerFactory: ILoggerFactory ) =
member __.loggerFactory = loggerFactory
interface INpgsqlLoggingProvider with
member __.CreateLogger(name) = loggerFactory.CreateLogger(name) |> SqlLogger :> NpgsqlLogger
然后像这样注册它:
let configureApp (app : IApplicationBuilder) =
NpgsqlLogManager.Provider <- SqlLoggerProvider (app.ApplicationServices.GetService<ILoggerFactory>())
NpgsqlLogManager.IsParameterLoggingEnabled <- true