服务边界上的WCF错误记录

时间:2022-04-24 03:07:53

I'm trying to implement an IErrorHandler in my WCF service in order to log every exception that hits the service boundary before it's passed to the client. I already use IErrorHandlers for translating Exceptions to typed FaultExceptions, which has been very useful. According to the MSDN for IErrorHandler.HandleError(), it's also intended to be used for logging at the boundary.

我正在尝试在我的WCF服务中实现IErrorHandler,以便记录在传递到客户端之前遇到服务边界的每个异常。我已经使用IErrorHandlers将Exceptions转换为类型化的FaultExceptions,这非常有用。根据IErrorHandler.HandleError()的MSDN,它也可用于边界记录。

The problem is, the HandleError function isn't guaranteed to be called on the operation thread, so I can't figure out how to get information about what operation triggered the exception. I can get the TargetSite out of the exception itself, but that gives me the interior method instead of the operation. I could also parse through the StackTrace string to figure out where it was thrown, but this seems a little fragile and hokey. Is there any consistent, supported way to get any state information (messages, operationdescription, anything) while in the HandleError function? Or any other ways to automatically log exceptions for service calls?

问题是,不保证在操作线程上调用HandleError函数,所以我无法弄清楚如何获取有关触发异常的操作的信息。我可以从异常中获取TargetSite,但这给了我内部方法而不是操作。我也可以通过StackTrace字符串解析它以确定它被抛出的位置,但这看起来有点脆弱和笨拙。在HandleError函数中是否有任何一致的,受支持的方式来获取任何状态信息(消息,操作描述,任何内容)?或者自动记录服务调用异常的任何其他方法?

I'm looking for a solution to implement on production, using my existing logging framework, so SvcTraceViewer won't do it for me.

我正在寻找一个使用我现有的日志框架实现生产的解决方案,所以SvcTraceViewer不会为我做这件事。

Thanks.

6 个解决方案

#1


3  

I ended up putting the logging in IErrorHandler.ProvideFault() instead of IErrorHandler.HandlerError(). The ProvideFault call is made in the operation thread, so I can use OperationContext.Current to get some information to log.

我最终将日志记录放在IErrorHandler.ProvideFault()而不是IErrorHandler.HandlerError()中。 ProvideFault调用是在操作线程中进行的,因此我可以使用OperationContext.Current来获取一些记录信息。

#2


2  

I use the IErrorHanlder in the same way that you describe, but not for logging. Instead on service classes (WCF or not) I use an interceptor as described here. I believe that this technique will capture the information you are interested in.

我以与您描述的方式相同的方式使用IErrorHanlder,但不用于记录。而不是服务类(WCF与否)我使用这里描述的拦截器。我相信这种技术将捕获您感兴趣的信息。

#3


2  

You could stash any context information you need to log in the Exception's Data dictionary in the ProvideFault method which is called on the operation thread...then reference it in the HandleError method for logging purposes.

您可以存储在操作线程上调用的ProvideFault方法中登录Exception的数据字典所需的任何上下文信息...然后在HandleError方法中引用它以进行日志记录。

#4


0  

Have you used the Service Trace Viewer?

您是否使用过服务跟踪查看器?

#5


0  

The ProvideFault() operations is being called on the incoming call thread and the client is still blocked waiting for response. I don't think it is good idea to add a lengthy process(like logging) inside this method. That is why they exposed another operation HandleError whch gets called on a separate worker thread.

正在传入调用线程上调用ProvideFault()操作,并且仍然阻止客户端等待响应。我不认为在此方法中添加冗长的进程(如日志记录)是个好主意。这就是为什么他们暴露了另一个操作HandleError whch在一个单独的工作线程上被调用的原因。

But I understand your situation. Please share if you found a solution other than logging inside ProvideFault.

但我了解你的情况。如果您在ProvideFault中登录以外的其他解决方案,请分享。

#6


0  

What about creating an instance and saving the request message on that instance?

如何在该实例上创建实例并保存请求消息?

#1


3  

I ended up putting the logging in IErrorHandler.ProvideFault() instead of IErrorHandler.HandlerError(). The ProvideFault call is made in the operation thread, so I can use OperationContext.Current to get some information to log.

我最终将日志记录放在IErrorHandler.ProvideFault()而不是IErrorHandler.HandlerError()中。 ProvideFault调用是在操作线程中进行的,因此我可以使用OperationContext.Current来获取一些记录信息。

#2


2  

I use the IErrorHanlder in the same way that you describe, but not for logging. Instead on service classes (WCF or not) I use an interceptor as described here. I believe that this technique will capture the information you are interested in.

我以与您描述的方式相同的方式使用IErrorHanlder,但不用于记录。而不是服务类(WCF与否)我使用这里描述的拦截器。我相信这种技术将捕获您感兴趣的信息。

#3


2  

You could stash any context information you need to log in the Exception's Data dictionary in the ProvideFault method which is called on the operation thread...then reference it in the HandleError method for logging purposes.

您可以存储在操作线程上调用的ProvideFault方法中登录Exception的数据字典所需的任何上下文信息...然后在HandleError方法中引用它以进行日志记录。

#4


0  

Have you used the Service Trace Viewer?

您是否使用过服务跟踪查看器?

#5


0  

The ProvideFault() operations is being called on the incoming call thread and the client is still blocked waiting for response. I don't think it is good idea to add a lengthy process(like logging) inside this method. That is why they exposed another operation HandleError whch gets called on a separate worker thread.

正在传入调用线程上调用ProvideFault()操作,并且仍然阻止客户端等待响应。我不认为在此方法中添加冗长的进程(如日志记录)是个好主意。这就是为什么他们暴露了另一个操作HandleError whch在一个单独的工作线程上被调用的原因。

But I understand your situation. Please share if you found a solution other than logging inside ProvideFault.

但我了解你的情况。如果您在ProvideFault中登录以外的其他解决方案,请分享。

#6


0  

What about creating an instance and saving the request message on that instance?

如何在该实例上创建实例并保存请求消息?