Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Created February 16, 2016 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendettamit/1619b854dad5282766cc to your computer and use it in GitHub Desktop.
Save vendettamit/1619b854dad5282766cc to your computer and use it in GitHub Desktop.
public class ExpensiveSqlLoggerInterceptor : DbCommandInterceptor
{
private readonly IQueryLogger _queryLogger;
private readonly int _executionMillisecondThreshold;
private readonly bool _includeStackTrace;
public ExpensiveSqlLoggerInterceptor(IQueryLogger logger, int executionMillisecondThreshold, bool enableStackTrace = true)
{
_queryLogger = logger;
_executionMillisecondThreshold = executionMillisecondThreshold;
_includeStackTrace = enableStackTrace;
}
public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
Executing(interceptionContext);
base.ReaderExecuting(command, interceptionContext);
}
public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
Executed(command, interceptionContext);
base.ReaderExecuted(command, interceptionContext);
}
public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
Executing(interceptionContext);
base.NonQueryExecuting(command, interceptionContext);
}
public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
Executed(command, interceptionContext);
base.NonQueryExecuted(command, interceptionContext);
}
public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
Executing(interceptionContext);
base.ScalarExecuting(command, interceptionContext);
}
public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
Executed(command, interceptionContext);
base.ScalarExecuted(command, interceptionContext);
}
private void Executing<T>(DbCommandInterceptionContext<T> interceptionContext)
{
var timer = new Stopwatch();
interceptionContext.UserState = timer;
timer.Start();
}
private void Executed<T>(DbCommand command, DbCommandInterceptionContext<T> interceptionContext)
{
var timer = (Stopwatch)interceptionContext.UserState;
timer.Stop();
if (interceptionContext.Exception != null)
{
_queryLogger.Write("FAILED COMMAND",
interceptionContext.Exception.Message,
command.CommandText,
_includeStackTrace ? Environment.StackTrace : string.Empty,
string.Empty,
string.Empty);
}
else if (timer.ElapsedMilliseconds >= _executionMillisecondThreshold)
{
_queryLogger.Write(
string.Format("SLOW COMMAND ({0} ms)",timer.ElapsedMilliseconds),
command.CommandText,
_includeStackTrace ? Environment.StackTrace : string.Empty,
string.Empty,
string.Empty
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment