Windsor와 같은 IoC 프레임 워크를 사용하는 경우 AOP (Aspect-oriented programming)를 사용하여 인터페이스에 래퍼를 삽입 할 수 있습니다. 그런 다음 메소드 호출을 기록 할 수 있습니다. 다음은 Windsor와 IInteceptor 인터페이스를 사용하는 예제입니다.이 인터페이스는 우리가 사용하는 스트라이프 다운 버전이지만 다양한 예제가 있습니다.
public class LoggingInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Type targetType = invocation.TargetType ?? invocation.Method.DeclaringType;
ILog logger = LogManager.GetLogger(targetType);
//Probably want to check logger.IsDebugEnabled
if(invocation.Arguments.Count() == 0)
{
logger.DebugFormat("Method '{0}' called.", invocation.Method);
}
else
{
var stringBuilder = new StringBuilder("{" + invocation.Arguments.Length + "}");
for (int i = invocation.Arguments.Length - 1; i > 0; i--)
{
stringBuilder.Insert(0, "{" + i + "}, ");
}
logger.DebugFormat("Method '{0}' called with parameters: " + stringBuilder, new[] { invocation.Method }.Union(invocation.Arguments).ToArray());
}
try
{
invocation.Proceed();
if (invocation.Method.ReturnType != typeof(void))
{
logger.DebugFormat("Method '{0}' returned: {1}", invocation.Method, invocation.ReturnValue);
}
}
catch(Exception ex)
{
logger.Error(string.Format("Method '{0}' threw exception:", invocation.Method), ex);
throw;
}
}
}
IoC 프레임 워크에 따라 개별 구성 요소를 연결할 수 있어야합니다.
POINT는 참고 :
이 아니라, 객체의 private 메소드를 연결할 수 없습니다 인터페이스 방법에 대한 작동합니다.