Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Dapper.Logging/Hooks/ISqlHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Dapper.Logging.Hooks
{
public interface ISqlHooks<in T>
{
void ConnectionOpened(DbConnection connection, T context, long elapsedMs);
void ConnectionClosed(DbConnection connection, T context, long elapsedMs);
void CommandExecuted(DbCommand command, T context, long elapsedMs);
void ConnectionOpened(DbConnection connection, T context, double elapsedMs);
void ConnectionClosed(DbConnection connection, T context, double elapsedMs);
void CommandExecuted(DbCommand command, T context, double elapsedMs);
}
}
35 changes: 20 additions & 15 deletions src/Dapper.Logging/Hooks/WrappedCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ internal class WrappedCommand<T> : DbCommand
private readonly T _context;

public WrappedCommand(
DbCommand command,
DbConnection connection,
ISqlHooks<T> hooks,
DbCommand command,
DbConnection connection,
ISqlHooks<T> hooks,
T context)
{
_command = command;
Expand All @@ -27,50 +27,50 @@ public WrappedCommand(

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
var reader = _command.ExecuteReader(behavior);
_hooks.CommandExecuted(this, _context, sw.ElapsedMilliseconds);
_hooks.CommandExecuted(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
return reader;
}

protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(
CommandBehavior behavior, CancellationToken cancellationToken)
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
var reader = await _command.ExecuteReaderAsync(behavior, cancellationToken);
_hooks.CommandExecuted(this, _context, sw.ElapsedMilliseconds);
_hooks.CommandExecuted(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
return reader;
}

public override int ExecuteNonQuery()
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
var result = _command.ExecuteNonQuery();
_hooks.CommandExecuted(this, _context, sw.ElapsedMilliseconds);
_hooks.CommandExecuted(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
return result;
}

public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
var result = await _command.ExecuteNonQueryAsync(cancellationToken);
_hooks.CommandExecuted(this, _context, sw.ElapsedMilliseconds);
_hooks.CommandExecuted(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
return result;
}

public override object ExecuteScalar()
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
var result = _command.ExecuteScalar();
_hooks.CommandExecuted(this, _context, sw.ElapsedMilliseconds);
_hooks.CommandExecuted(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
return result;
}

public override async Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
var result = await _command.ExecuteScalarAsync(cancellationToken);
_hooks.CommandExecuted(this, _context, sw.ElapsedMilliseconds);
_hooks.CommandExecuted(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
return result;
}

Expand Down Expand Up @@ -129,6 +129,11 @@ protected override void Dispose(bool disposing)
_command?.Dispose();

base.Dispose(disposing);
}

double GetElapsedMilliseconds(long start, long stop)
{
return (stop - start) * 1000 / (double)Stopwatch.Frequency;
}
}
}
17 changes: 11 additions & 6 deletions src/Dapper.Logging/Hooks/WrappedConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public WrappedConnection(DbConnection connection, ISqlHooks<T> hooks, T context)

public override void Close()
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
_connection.Close();
_hooks.ConnectionClosed(this, _context, sw.ElapsedMilliseconds);
_hooks.ConnectionClosed(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
}

public override void Open()
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
_connection.Open();
_hooks.ConnectionOpened(this, _context, sw.ElapsedMilliseconds);
_hooks.ConnectionOpened(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
}

public override async Task OpenAsync(CancellationToken cancellationToken)
{
var sw = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
await _connection.OpenAsync(cancellationToken);
_hooks.ConnectionOpened(this, _context, sw.ElapsedMilliseconds);
_hooks.ConnectionOpened(this, _context, GetElapsedMilliseconds(start, Stopwatch.GetTimestamp()));
}

protected override DbCommand CreateDbCommand() =>
Expand Down Expand Up @@ -70,6 +70,11 @@ protected override void Dispose(bool disposing)
_connection?.Dispose();

base.Dispose(disposing);
}

double GetElapsedMilliseconds(long start, long stop)
{
return (stop - start) * 1000 / (double)Stopwatch.Frequency;
}
}
}
6 changes: 3 additions & 3 deletions src/Dapper.Logging/LoggingHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ public LoggingHook(ILogger logger, DbLoggingConfiguration config)
_connectionProjector = _config.ConnectionProjector ?? (_ => Empty.Object);
}

public void ConnectionOpened(DbConnection connection, T context, long elapsedMs) =>
public void ConnectionOpened(DbConnection connection, T context, double elapsedMs) =>
_logger.Log(
_config.LogLevel,
_config.OpenConnectionMessage,
elapsedMs,
context,
_connectionProjector(connection));

public void ConnectionClosed(DbConnection connection, T context, long elapsedMs) =>
public void ConnectionClosed(DbConnection connection, T context, double elapsedMs) =>
_logger.Log(
_config.LogLevel,
_config.CloseConnectionMessage,
elapsedMs,
context,
_connectionProjector(connection));

public void CommandExecuted(DbCommand command, T context, long elapsedMs) =>
public void CommandExecuted(DbCommand command, T context, double elapsedMs) =>
_logger.Log(
_config.LogLevel,
_config.ExecuteQueryMessage,
Expand Down