Created
September 22, 2020 15:26
-
-
Save smailliwcs/4d00a86ec0c9bbed27a1784cfbdfbb47 to your computer and use it in GitHub Desktop.
Logging SQL queries in ADO.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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