Created
July 3, 2012 14:55
-
-
Save kstenson/3040235 to your computer and use it in GitHub Desktop.
Log4Net interceptor
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 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