Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Last active August 29, 2015 14:21
Show Gist options
  • Save cmatskas/5cdeb78fe00acfb21842 to your computer and use it in GitHub Desktop.
Save cmatskas/5cdeb78fe00acfb21842 to your computer and use it in GitHub Desktop.
using Common.Logging;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Data.Entity.SqlServer;
using System.Data.SqlClient;
using System.Reflection;
using System.Linq;
public class CustomEFInterceptor : IDbCommandInterceptor
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
WriteLog(string.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
WriteLog(string.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContextt<DbDataReader> interceptionContext)
{
WriteLog(string.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
WriteLog(string.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
WriteLog(string.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
WriteLog(string.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}
private void WriteLog(string command)
{
Log.Debug(m => m(command));
// or use the verbose option
/*if (Log.IsDebugEnabled)
{
Log.Debug(command);
}*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment