views:

7

answers:

1

I implemented a class that implements IErrorHandler interface to log WCF errors. One of the things that I'd like to do is log who the identity of the user than connected to my service when an exception occurred. All my logging occurs in HandleError() method of IErrorHandler interface, but since HandleError() may not have current operation context, I can't get the SecurityContext.PrimaryIdentity. I've come up with the following code to capture things that may not be available in HandleError method, but I'm not sure this will work in all cases.

public class MyErrorHandler : IErrorHandler
{
   private IIdentity identity;

   public bool HandleError(Exception error)
   {
       // Do something with identity          

       return false;
   }

   public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
   {
       this.identity = Operation.Current.SecurityContext.PrimaryIdentity;
   }
}

The code above seems to be working, but are there any gotchas?

Thanks!

A: 

For some reason, I thought that the class was created every time there was an exception. In any case, I was able to solve my issue by using Dictionary property of Exception object to store custom data that I wanted to log with my exception.

Eric