开发者

Question on logging errors in WCF

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 o开发者_JAVA百科ccurs 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜