views:

434

answers:

2

Hello,

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.

I've seen http://stackoverflow.com/questions/238755/castle-aop-and-logging-in-net, but it only covers method's parameters.

Is there a better (WCF or Castle's) mechanism for doing this? BTW I'm using log4net for logging.

UPDATE: I know about WCF's logging facilities, but I was looking more for a general solution, not just in WCF environment.

Thanks in advance.

+1  A: 

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.

http://www.devx.com/dotnet/Article/37389/0/page/6

There is the IErrorHandler interface in WCF:

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

Jonathan Parker
Ok, but let's generalize the question: how would you do it in a non-WCF environment?
Igor Brejc
I've updated my answer.
Jonathan Parker
+2  A: 

If you want to use Castle Interceptors in general, you could do something like this:

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
   }
}
Krzysztof Koźmic