-
Notifications
You must be signed in to change notification settings - Fork 0
SQL logging
MSugiura edited this page Dec 27, 2023
·
1 revision
RedOrb's DB operations are performed via extension methods of the IDbConnection class. Since the database connection class is not hidden, you can easily implement your own SQL logging processing by inserting the logging function into the database connection class itself.
If you want to do logging easily, please use LoggingDbConnection. This class is a wrapper class for IDbConnection and ILogger.
SQL logging sample with xUnit.
public UnitTest1(ITestOutputHelper output)
{
var logger = new UnitTestLogger() { Output = output };
var cn = new NpgsqlConnection("connection string");
var lcn = new LoggingDbConnection(cn, logger);
}
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
namespace PostgresSample;
public class UnitTestLogger : ILogger
{
public required ITestOutputHelper Output { get; init; }
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Output.WriteLine($"{DateTime.Now:HH:mm:ss.fff} [{logLevel}] {formatter(state, exception)}");
}
}