DbQueryLogging 1.0.1

A simple wrapper on DbConnection and DbCommand that logs SQL statements into an ILogger instance.

Install-Package DbQueryLogging -Version 1.0.1
dotnet add package DbQueryLogging --version 1.0.1
<PackageReference Include="DbQueryLogging" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DbQueryLogging --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DbQueryLogging, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install DbQueryLogging as a Cake Addin
#addin nuget:?package=DbQueryLogging&version=1.0.1

// Install DbQueryLogging as a Cake Tool
#tool nuget:?package=DbQueryLogging&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DbQueryLogging

A simple wrapper on DbConnection and DbCommand that logs SQL statements into an ILogger instance

.NET Core

Nuget Package

.Net Standard 2.1

https://www.nuget.org/packages/DbQueryLogging/

Usage

Use as the following:

var connection = new LoggedDbConnection(DbConnection, ILogger);

Where

DbConnection : System.Data.Common.DbConnection

ILogger : Microsoft.Extensions.Logging.ILogger

Demo sample

A sample demo project is also available in the sources at :
https://github.com/akarzazi/DbQueryLogging/tree/master/demo

using System;
using DbQueryLogging;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging;

namespace Demo
{
    public class Program
    {
        const string connectionString = "Data Source=InMemorySample;Mode=Memory;Cache=Shared";

        static void Main(string[] args)
        {
            ILogger consoleLogger = new ConsoleLogger();

            using var connection = new LoggedDbConnection(new SqliteConnection(connectionString), consoleLogger);
            connection.Open();

            var queryCommand = connection.CreateCommand();
            queryCommand.CommandText = "SELECT @name, @age";
            queryCommand.Parameters.AddRange(new[] {
                    new SqliteParameter("@name", "Bob"),
                    new SqliteParameter("@age", 28)
                });
            var result = queryCommand.ExecuteScalar();
        }

        public class ConsoleLogger : ILogger
        {
            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            {
                Console.WriteLine(formatter(state, exception));
            }

            public IDisposable BeginScope<TState>(TState state) => throw new NotImplementedException();
            public bool IsEnabled(LogLevel logLevel) => true;
        }
    }
}

Output

Executing
- CommandType Text
- CommandText SELECT @name, @age
- Parameters
        @name = Bob
        @age = 28

DbQueryLogging

A simple wrapper on DbConnection and DbCommand that logs SQL statements into an ILogger instance

.NET Core

Nuget Package

.Net Standard 2.1

https://www.nuget.org/packages/DbQueryLogging/

Usage

Use as the following:

var connection = new LoggedDbConnection(DbConnection, ILogger);

Where

DbConnection : System.Data.Common.DbConnection

ILogger : Microsoft.Extensions.Logging.ILogger

Demo sample

A sample demo project is also available in the sources at :
https://github.com/akarzazi/DbQueryLogging/tree/master/demo

using System;
using DbQueryLogging;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging;

namespace Demo
{
    public class Program
    {
        const string connectionString = "Data Source=InMemorySample;Mode=Memory;Cache=Shared";

        static void Main(string[] args)
        {
            ILogger consoleLogger = new ConsoleLogger();

            using var connection = new LoggedDbConnection(new SqliteConnection(connectionString), consoleLogger);
            connection.Open();

            var queryCommand = connection.CreateCommand();
            queryCommand.CommandText = "SELECT @name, @age";
            queryCommand.Parameters.AddRange(new[] {
                    new SqliteParameter("@name", "Bob"),
                    new SqliteParameter("@age", 28)
                });
            var result = queryCommand.ExecuteScalar();
        }

        public class ConsoleLogger : ILogger
        {
            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            {
                Console.WriteLine(formatter(state, exception));
            }

            public IDisposable BeginScope<TState>(TState state) => throw new NotImplementedException();
            public bool IsEnabled(LogLevel logLevel) => true;
        }
    }
}

Output

Executing
- CommandType Text
- CommandText SELECT @name, @age
- Parameters
        @name = Bob
        @age = 28

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.1 151 4/27/2020
1.0.0 151 4/24/2020