我有一个与 WCF 服务通信的 Windows 服务。WCF 服务都是故障屏蔽的,并生成自定义 UserFaultContracts 和 ServiceFaultContracts。那里没有问题。
在 Windows 服务中,我使用 EntLib 进行异常处理和日志记录。
我不想尝试捕捉错误
try
{
}
catch (FaultException<UserFaultContract>)
{
}
我想使用 EntLib
try
{
}
catch (Exception ex)
{
var rethrow = ExceptionPolicy.HandleException(ex, "Transaction Policy");
if (rethrow) throw;
}
但是,这也适用于我的交易策略,我想记录 UserFaultContract 的详细信息。这是我脱胶的地方。我讨厌脱胶。故障已被捕获并记录……但我无法获得故障的详细信息。
我的例外政策是
<add name="Transaction Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="200" severity="Error" title="Transaction Error"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
priority="2" useDefaultLogger="true" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Logging Handler" />
</exceptionHandlers>
</add>
<add type="System.ServiceModel.FaultException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None" name="FaultException">
<exceptionHandlers>
<add logCategory="General" eventId="200" severity="Error" title="Service Fault"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
priority="2" useDefaultLogger="true" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Logging Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
记录的异常是:
时间戳:5/13/2010 14:53:40 消息:HandlingInstanceID:e9038634-e16e-4d87-ab1e-92379431838b
'System.ServiceModel.FaultException`1[[LCI.DispatchMaster.FaultContracts.ServiceFaultContract, LCI.DispatchMaster.FaultContracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 类型的异常发生并被捕获。
2010 年 5 月 13 日 10:53:40 类型:System.ServiceModel.FaultException`1[[LCI.DispatchMaster.FaultContracts.ServiceFaultContract,LCI.DispatchMaster.FaultContracts,版本=1.0.0.0,文化=中性,PublicKeyToken=null]] , System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 消息:DispatchMaster 服务出现内部故障。来源:mscorlib 帮助链接:详细信息:LCI.DispatchMaster.FaultContracts.ServiceFaultContract 操作:http: //LCI.DispatchMaster.LogicalChoices.com/ITruckMasterService/MergeScenarioServiceFaultContractFault 代码:System.ServiceModel.FaultCode 原因:DispatchMaster 服务出现内部故障。数据:System.Collections.ListDictionaryInternal TargetSite:无效HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage,System.Runtime.Remoting.Messaging.IMessage)堆栈跟踪:
在故障触点中有一个 ID 和一条消息。如您所见,我会喜欢 EntLib 记录的 ID 和消息。
我假设我将不得不编写一个自定义处理程序来提取故障详细信息 - 但我想我会问我是否在 EntLib 中遗漏了一些可以帮助我避免该任务的东西。
感谢任何愿意提供帮助的人。