I'm writing a WCF web service and I'm wondering if there's an elegant (aspect-oriented) way to use Castle interceptor mechanism for logging exceptions thrown by my web methods? I know about the IInterceptor
inteface, but I could not find any exception information there.
我正在编写一个WCF Web服务,我想知道是否有一种优雅(面向方面)的方法来使用Castle拦截器机制来记录我的Web方法抛出的异常?我知道IInterceptor接口,但我找不到任何异常信息。
I've seen Castle, AOP and Logging in .NET, but it only covers method's parameters.
我见过Castle,AOP和Logging in .NET,但它只涵盖了方法的参数。
Is there a better (WCF or Castle's) mechanism for doing this? BTW I'm using log4net for logging.
这样做有更好的(WCF或城堡)机制吗?顺便说一句,我正在使用log4net进行日志记录。
UPDATE: I know about WCF's logging facilities, but I was looking more for a general solution, not just in WCF environment.
更新:我知道WCF的日志记录工具,但我正在寻找更多的通用解决方案,而不仅仅是在WCF环境中。
Thanks in advance.
提前致谢。
2 个解决方案
#1
You should use the build in WCF tracing and logging. Then you can use the WCF Trace Viewer which will piece together logs from service and client so that you can see the full message flow.
您应该在WCF跟踪和日志记录中使用构建。然后,您可以使用WCF跟踪查看器,它将来自服务和客户端的日志拼凑在一起,以便您可以看到完整的消息流。
http://www.devx.com/dotnet/Article/37389/0/page/6
There is the IErrorHandler interface in WCF:
WCF中有IErrorHandler接口:
public interface IErrorHandler
{
bool HandleError(Exception error, MessageFault fault);
void ProvideFault(Exception error, ref MessageFault fault, ref string faultAction);
}
More details here: http://www.extremeexperts.com/Net/Articles/ExceptionHandlingInWCF.aspx
更多细节:http://www.extremeexperts.com/Net/Articles/ExceptionHandlingInWCF.aspx
#2
If you want to use Castle Interceptors in general, you could do something like this:
如果你想一般使用Castle Interceptors,你可以这样做:
public void Intercept(IInvocation invocation)
{
//do some preprocessing here if needed
try
{
invocation.Proceed()
}
catch(Exception e)
{
LogException(e);
throw;
}
finally
{
//do some postprocessing jf needed
}
}
#1
You should use the build in WCF tracing and logging. Then you can use the WCF Trace Viewer which will piece together logs from service and client so that you can see the full message flow.
您应该在WCF跟踪和日志记录中使用构建。然后,您可以使用WCF跟踪查看器,它将来自服务和客户端的日志拼凑在一起,以便您可以看到完整的消息流。
http://www.devx.com/dotnet/Article/37389/0/page/6
There is the IErrorHandler interface in WCF:
WCF中有IErrorHandler接口:
public interface IErrorHandler
{
bool HandleError(Exception error, MessageFault fault);
void ProvideFault(Exception error, ref MessageFault fault, ref string faultAction);
}
More details here: http://www.extremeexperts.com/Net/Articles/ExceptionHandlingInWCF.aspx
更多细节:http://www.extremeexperts.com/Net/Articles/ExceptionHandlingInWCF.aspx
#2
If you want to use Castle Interceptors in general, you could do something like this:
如果你想一般使用Castle Interceptors,你可以这样做:
public void Intercept(IInvocation invocation)
{
//do some preprocessing here if needed
try
{
invocation.Proceed()
}
catch(Exception e)
{
LogException(e);
throw;
}
finally
{
//do some postprocessing jf needed
}
}