在VB.NET中登录Npgsql

问题描述 投票:1回答:1

我曾经使用npgsql 2.2.5,并记录了npgsql所做的所有事情,我使用了以下内容:

NpgsqlEventLog.Level = LogLevel.Normal
NpgsqlEventLog.LogName = ("c:\npgsql.log")
NpgsqlEventLog.EchoMessages = True

否,对于3.2.5版,说明(在C#中为:

NpgsqlLogManager.Provider = new ConsoleLoggingProvider(<min level>, <print level?>, <print connector id?>);

我不了解C#,它说要把它放在您的应用程序的开头(请参阅info page.),我需要将其放入网站中。

我应该在网站的哪个位置放置此代码,以及如何使npgsql将日志发送到文本文件?

c# vb.net postgresql npgsql
1个回答
0
投票

我使用了以下内容:

dlLoggingNpgSQL:

Imports Npgsql
Imports NLog
Imports Npgsql.Logging

Namespace dlLoggingNpgSQL
Public Class NLogLoggingProvider
    Implements INpgsqlLoggingProvider

    Public Function INpgsqlLoggingProvider_CreateLogger(name As String) As NpgsqlLogger Implements INpgsqlLoggingProvider.CreateLogger
        Return New NLogLogger(name)
    End Function
End Class

Public Class NLogLogger
    Inherits NpgsqlLogger

    ReadOnly _log As Logger

    Public Sub New(name As String)
        _log = LogManager.GetLogger(name)
    End Sub

    Public Overrides Function IsEnabled(level As NpgsqlLogLevel) As Boolean
        Return _log.IsEnabled(ToNLogLogLevel(level))
    End Function

    Public Overrides Sub Log(level As NpgsqlLogLevel, connectorId As Integer, msg As String, Optional exception As Exception = Nothing)
        Dim ev = New LogEventInfo(ToNLogLogLevel(level), "", msg)
        If exception Is Nothing Then
            ev.Exception = exception
        End If

        If connectorId <> 0 Then
            ev.Properties("ConnectorId") = connectorId
        End If

        _log.Log(ev)
        _log.Log(ev.Level, msg)
    End Sub

    Public Function ToNLogLogLevel(level As NpgsqlLogLevel) As LogLevel
        Select Case level
            Case NpgsqlLogLevel.Trace
                Return LogLevel.Trace
            Case NpgsqlLogLevel.Debug
                Return LogLevel.Debug
            Case NpgsqlLogLevel.Info
                Return LogLevel.Info
            Case NpgsqlLogLevel.Warn
                Return LogLevel.Warn
            Case NpgsqlLogLevel.Error
                Return LogLevel.Error
            Case NpgsqlLogLevel.Fatal
                Return LogLevel.Fatal
            Case Else
                Throw New ArgumentOutOfRangeException("level")
        End Select
    End Function
End Class
End Namespace

dlNpgSQLwithUsing:

Imports NLog
Imports Npgsql
Imports Npgsql.Logging


Public Class dlNpgSQLwithUsing
Dim _connectionString As String
Dim _sqlConnection As NpgsqlConnection
Dim _sqlCommand As NpgsqlCommand
Dim _sqlDataAdapter As NpgsqlDataAdapter
Dim _dataset As DataSet
Dim _datatable As DataTable

Private _NLogLoggingProvider As New NLogLoggingProvider
Private Sub InitializeLogggingProvider()
    If NpgsqlLogManager.IsParameterLoggingEnabled = False Then
        NpgsqlLogManager.IsParameterLoggingEnabled = True
        NpgsqlLogManager.Provider = New NLogLoggingProvider()
    End If
End Sub

Public Function GetConnectionStringByName(Optional ByVal ConnectionString As String = "")
    If String.IsNullOrEmpty(ConnectionString) Then
        Return ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Else
        Return ConfigurationManager.ConnectionStrings(ConnectionString).ConnectionString
    End If
End Function

Public Function GetDataSet(ByVal connectionStringName As String, ByVal strQuery As String) As DataSet

    _dataset = New DataSet

    Try
        _connectionString = GetConnectionStringByName(connectionStringName)
        _NLogLoggingProvider.INpgsqlLoggingProvider_CreateLogger("Npgsql.NpgsqlConnection")

        Using _connection As New NpgsqlConnection(_connectionString)
            Using _sqlDataAdapter = New NpgsqlDataAdapter(strQuery, _connection)
                _sqlDataAdapter.Fill(_dataset)
            End Using
        End Using
    Catch ex As Exception
        Throw New System.Exception(ex.Message)
    End Try

    Return _dataset
End Function

Public Function GetDataTable(ByVal connectionStringName As String, ByVal strQuery As String) As DataTable

    _datatable = New DataTable

    Try
        _connectionString = GetConnectionStringByName(connectionStringName)
        _NLogLoggingProvider.INpgsqlLoggingProvider_CreateLogger("Npgsql.NpgsqlConnection")

        Using _connection As New NpgsqlConnection(_connectionString)
            Using _sqlDataAdapter = New NpgsqlDataAdapter(strQuery, _connection)
                _sqlDataAdapter.Fill(_datatable)
            End Using
        End Using
    Catch ex As Exception
        Throw New System.Exception(ex.Message)
    End Try

    Return _datatable
End Function


Public Function ExecuteQuery(ByVal connectionStringName As String, ByVal strQuery As String) As String

    Dim RecordsReturned As String = ""

    Try
        _connectionString = GetConnectionStringByName(connectionStringName)
        _NLogLoggingProvider.INpgsqlLoggingProvider_CreateLogger("Npgsql.NpgsqlConnection")

        Using _connection As New NpgsqlConnection(_connectionString)
            _connection.Open()
            _sqlCommand = New NpgsqlCommand(strQuery, _connection)
            RecordsReturned = _sqlCommand.ExecuteNonQuery()
            _connection.Close()
        End Using

    Catch ex As Exception
       Throw
    End Try

    Return RecordsReturned

End Function

Public Function ExecuteScalar(ByVal connectionStringName As String, ByVal strQuery As String) As String

    Dim RecordsReturned As String = ""

    Try
        _connectionString = GetConnectionStringByName(connectionStringName)
        _NLogLoggingProvider.INpgsqlLoggingProvider_CreateLogger("Npgsql.NpgsqlConnection")

        Using _connection As New NpgsqlConnection(_connectionString)
            _connection.Open()
            _sqlCommand = New NpgsqlCommand(strQuery, _connection)
            RecordsReturned = _sqlCommand.ExecuteScalar()
            _connection.Close()
        End Using

    Catch ex As Exception
        Throw
    End Try

    Return RecordsReturned
End Function

End Class

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.