Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Logging SQL queries in ADO.NET
using System.Data;
public class LoggingCommand : IDbCommand
{
private static void Log(string message)
{
// ...
}
private IDbCommand @base;
public string CommandText
{
get { return @base.CommandText; }
set { @base.CommandText = value; }
}
public int CommandTimeout
{
get { return @base.CommandTimeout; }
set { @base.CommandTimeout = value; }
}
public CommandType CommandType
{
get { return @base.CommandType; }
set { @base.CommandType = value; }
}
public IDbConnection Connection
{
get { return @base.Connection; }
set { @base.Connection = value; }
}
public IDataParameterCollection Parameters
{
get { return @base.Parameters; }
}
public IDbTransaction Transaction
{
get { return @base.Transaction; }
set { @base.Transaction = value; }
}
public UpdateRowSource UpdatedRowSource
{
get { return @base.UpdatedRowSource; }
set { @base.UpdatedRowSource = value; }
}
public LoggingCommand(IDbCommand @base)
{
this.@base = @base;
}
public void Cancel()
{
@base.Cancel();
}
public IDbDataParameter CreateParameter()
{
return @base.CreateParameter();
}
public void Dispose()
{
@base.Dispose();
}
private void OnExecuting()
{
Log(CommandText);
}
public int ExecuteNonQuery()
{
OnExecuting();
return @base.ExecuteNonQuery();
}
public IDataReader ExecuteReader()
{
OnExecuting();
return @base.ExecuteReader();
}
public IDataReader ExecuteReader(CommandBehavior behavior)
{
OnExecuting();
return @base.ExecuteReader(behavior);
}
public object ExecuteScalar()
{
OnExecuting();
return @base.ExecuteScalar();
}
public void Prepare()
{
@base.Prepare();
}
}
using System.Data;
public class LoggingConnection : IDbConnection
{
private IDbConnection @base;
public string ConnectionString
{
get { return @base.ConnectionString; }
set { @base.ConnectionString = value; }
}
public int ConnectionTimeout
{
get { return @base.ConnectionTimeout; }
}
public string Database
{
get { return @base.Database; }
}
public ConnectionState State
{
get { return @base.State; }
}
public LoggingConnection(IDbConnection @base)
{
this.@base = @base;
}
public IDbTransaction BeginTransaction()
{
return @base.BeginTransaction();
}
public IDbTransaction BeginTransaction(IsolationLevel il)
{
return @base.BeginTransaction(il);
}
public void ChangeDatabase(string databaseName)
{
@base.ChangeDatabase(databaseName);
}
public void Close()
{
@base.Close();
}
public IDbCommand CreateCommand()
{
return new LoggingCommand(@base.CreateCommand());
}
public void Dispose()
{
@base.Dispose();
}
public void Open()
{
@base.Open();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.