我将 Dapper 与 Postgres 一起使用,我想记录一些诊断信息,尤其是正在运行的实际查询。
如果我使用 SQL Server,我会这样做
let connection = new SqlConnection(connectionString)
connection.StatisticsEnabled = true
// run query
let stats = connection.RetrieveStatistics()
但我似乎找不到类似的东西
NpgsqlConnection
。
检索正在运行的实际查询的最佳方法是什么?
然后像这样注册它:
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