使用IErrorHandler在HandleError或ProvideFault中记录WCF中的异常? [英] Logging exceptions in WCF with IErrorHandler inside HandleError or ProvideFault?
问题描述
我可以看到让用户引起异常的唯一方法是:
OperationContext.Current.IncomingMessageProperties.Security.ServiceSecurityContext.PrimaryIdentity
...但这只是似乎在ProvideFault内部工作,而不是HandleError内部。有没有办法让用户在HandleError内部?我想使用HandleError而不是ProvideFault,就像在后台线程上调用的那样,用于错误记录,对吧?
IErrorHandler的两种方法具有相当明确的职责:
-
HandleError 在这里处理所有未被捕获例外 - 这就是为什么它是做日志记录的最佳地方 - 这真的是它的全部原因。
-
ProvideFault 的任务是转向您的.NET异常成为可互操作的SOAP错误 - 或者忽略全部的异常
当然,没有什么技术上阻止你从您在 ProvideFault
方法中进行登录时,如果我不得不寻找这个方法,那么我不会去找那个功能。我倾向于遵循最不重要的原则 - 如果方法称为 ProvideFault
,我只希望它提供一个 FaultException< T> / code> - 不要做很多其他的事情。
要访问您的服务的安全上下文,请使用以下代码片段:
ServiceSecurityContext secCtx = ServiceSecurityContext.Current;
/ pre>
if(secCtx.PrimaryIdentity!= null)
{
//做某些主要身份
}
I'm using IErrorHandler to do exception handling in WCF and now I want to log the exceptions, along with the stack trace and the user that caused the exception.
The only way I can see to get the user that caused the exception is:
OperationContext.Current.IncomingMessageProperties.Security.ServiceSecurityContext.PrimaryIdentity
...But this only seems to work inside ProvideFault, and not inside HandleError. Is there a way to get the user inside HandleError? I would like to use HandleError instead of ProvideFault as that's called on a background thread and meant for error logging, right?
解决方案The two methods of the IErrorHandler have quite well defined responsibilities:
HandleError is here to handle all uncaught exceptions - that's why it's the best place to do your logging - that's really its whole reason to be
ProvideFault is tasked with turning your .NET exception into an interoperable SOAP fault - or ignore the exception alltogether
Of course, there's nothing technically stopping you from doing your logging in the ProvideFault
method - it's just not the place I would go look for that functionality if I ever had to look for it. I tend to like to follow to principle of least surprise - if the method is called ProvideFault
, I only expect it to provide a FaultException<T>
- not do a lot of other things, too.
To get access to your service's security context, use this snippet of code:
ServiceSecurityContext secCtx = ServiceSecurityContext.Current;
if(secCtx.PrimaryIdentity != null)
{
// do something with primary identity
}
这篇关于使用IErrorHandler在HandleError或ProvideFault中记录WCF中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!