【发布时间】:2012-05-17 20:51:21
【问题描述】:
我正在使用Log4Net来记录我的应用程序。目前我想记录在我的应用程序中输入的每个方法(用于测试目的)。因此我使用AutoFac 拦截功能,像这样:
builder.Register(c=> new MyClass()).As<IMyInterface>().EnableInterfaceInterceptors().InterceptedBy(typeof(LoggerClass));
builder.Build();
我的LoggerClass 看起来像这样:
public class LoggerClass : StandartInterceptor
{
ILog _log = LogManager.GetLogger(typeof(LoggerClass));
override void PreProceed(IInvocation invovation)
{
_log.Info(string.Format("entering method: {0}",invocation.Method.Name);
}
}
目前,此实现将为所有方法调用打印消息(拦截器捕获所有方法条目)。
问题
我想使用这种拦截机制来记录每个处理的异常。 例如,而不是这样编码:
catch (myException ex)
{
_log.Error(string.Format("catches exception {0}", ex.Message));
}
我将在我的LoggerClass 中有额外的方法来包装 catch 语句并向其注入日志消息。
有没有办法使用Log4Net 来做到这一点?因为基本上拦截器围绕该方法工作,我需要它在方法内部工作。
【问题讨论】: