Skip to content

Instantly share code, notes, and snippets.

@kstenson
Created July 3, 2012 14:55
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 kstenson/3040235 to your computer and use it in GitHub Desktop.
Save kstenson/3040235 to your computer and use it in GitHub Desktop.
Log4Net interceptor
public class LoggingInterceptor: IInterceptor
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public void Intercept(IInvocation invocation)
{
try
{
StringBuilder sb = null;
if (Logger.IsDebugEnabled)
{
sb = new StringBuilder(invocation.TargetType.FullName)
.Append(".")
.Append(invocation.Method)
.Append("(");
for (int i = 0; i < invocation.Arguments.Length; i++)
{
if (i > 0)
sb.Append(", ");
sb.Append(invocation.Arguments[i]);
}
sb.Append(")");
Logger.Debug(sb);
}
invocation.Proceed();
if (Logger.IsDebugEnabled)
{
if (invocation.ReturnValue != null && invocation.ReturnValue is IEnumerable)
{
dynamic collection = invocation.ReturnValue;
Logger.DebugFormat("Result of {0} is : Collection of {1} items", sb, collection.Count);
}
else
{
Logger.Debug("Result of " + sb + " is: " + invocation.ReturnValue);
}
}
}
catch (Exception e)
{
Logger.Error(e);
throw;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment