Created
October 11, 2017 15:27
-
-
Save cosoria/5da397a2ef626d7a140abf86f6533f25 to your computer and use it in GitHub Desktop.
NLog Command Interceptor allows to see queries on the logs
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
public class NLogDbCommandInterceptor : IDbCommandInterceptor | |
{ | |
private ILogger _logger; | |
private readonly Func<NLogDbCommandInterceptor, ILogger> _loggerFactory = interceptor => LoggerFactory.Instance.Create(interceptor); | |
private ILogger Logger | |
{ | |
get | |
{ | |
if (_logger == null) | |
{ | |
_logger = _loggerFactory(this); | |
} | |
return _logger; | |
} | |
} | |
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) | |
{ | |
LogIfNonAsync(command, interceptionContext); | |
} | |
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) | |
{ | |
LogIfError(command, interceptionContext); | |
} | |
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) | |
{ | |
LogIfNonAsync(command, interceptionContext); | |
} | |
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) | |
{ | |
LogIfError(command, interceptionContext); | |
} | |
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) | |
{ | |
LogIfNonAsync(command, interceptionContext); | |
} | |
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) | |
{ | |
LogIfError(command, interceptionContext); | |
} | |
private void LogIfNonAsync<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext) | |
{ | |
if (!interceptionContext.IsAsync) | |
{ | |
Logger.LogDebug("Executing Data Services Method:" + GetExecutingDataServiceMethod()); | |
Logger.LogDebug("Non-async command used: {0}", command.CommandText); | |
} | |
} | |
private void LogIfError<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext) | |
{ | |
if (interceptionContext.Exception != null) | |
{ | |
Logger.LogError("Command {0} failed with exception {1}", command.CommandText, interceptionContext.Exception); | |
} | |
} | |
private string GetExecutingDataServiceMethod() | |
{ | |
var isInfrastructureMethod = true; | |
var stackFrameNumber = 1; | |
var methodName = string.Empty; | |
while (isInfrastructureMethod) | |
{ | |
var callingMethod = new StackFrame(stackFrameNumber, false).GetMethod(); | |
if (callingMethod != null && | |
callingMethod.DeclaringType != null && | |
!callingMethod.DeclaringType.FullName.StartsWith("System.") && | |
!callingMethod.DeclaringType.FullName.Contains("Infrastructure")) | |
{ | |
methodName = string.Format("{0}.{1}", callingMethod.DeclaringType.FullName, callingMethod.Name); | |
isInfrastructureMethod = false; | |
} | |
stackFrameNumber++; | |
} | |
return methodName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment