[Source: http://geekswithblogs.net/EltonStoneman]
The management portal for ESB Guidance is an excellent tool which displays information on faults generated during the processing of messages through the ESB. It also exposes Web and WCF services for logging your own faults to the exception database, so you can record exceptions that occur in your own service providers and use the portal as a single view over the health of your SOA stack.
The SubmitFault method of the ExceptionManagement service takes a FaultMessage object which must be correctly configured with Header and FaultException properties. It takes some trial-and-error to work out what needs populating – if any properties are missing or invalid, your fault won't be logged. I've put together a sample error handler on MSDN Code Gallery which populates a call correctly: ESB Guidance Error Handler, which you can use as-is in your WCF services, or as a basis for your own handler.
In the sample, the error handler is available to your service so you can manually log a fault for any exceptions that you catch, and you can also configure it as a service behaviour so it will log any uncaught exceptions in your WCF service. A sample service and client are provided for demonstration – set up the SampleService as a virtual directory and run SampleClient (you'll need the ESB Guidance Exception Management installed).
To use the ServiceProviderErrorHandler add a config section in your web.config to specify how faults are logged:
<configSections>
<section
name="serviceProviderErrorHandlerConfiguration"
type="ESBGuidanceErrorHandler.Configuration.ServiceProviderErrorHandlerConfiguration, ESBGuidanceErrorHandler"/>
</configSections>
<serviceProviderErrorHandlerConfiguration
exceptionHandlingUrl="http://localhost/ESB.ExceptionHandlingServices/ExceptionHandling.asmx"
bizTalkApplication="BizTalk Application 1"
faultCode="50054"
errorType="Service Provider exception"
failureCategory="0"
faultGeneratorName="ESBGuidanceErrorHandler">
</serviceProviderErrorHandlerConfiguration>
This identifies the location of the ExceptionHandling service (the sample uses the SOAP entry point, but it's a simple change to use the WCF version as they have the same signature) and the descriptive text to categorise errors being logged, which will be shown in the portal. Note that the specified BizTalk Application must exist and the fault generator name is limited to 50 characters. Other values can contain any identifiers you like.
With the config specified, it's a simple call to log a fault – passing the name of the service provider and the service, description and severity of the fault, and an exception:
ServiceProviderErrorHandler.SubmitFault("ErroringService", "LogHandledException", "Caught exception", FaultSeverity.Error, ex);
To register the ServiceProviderErrorHandler to catch any unhandled exceptions that occur just requires a behaviour extension in the WCF service configuration:
<system.serviceModel>
<services>
<service name="ErroringService" behaviorConfiguration="defaultServiceBehavior">
<endpoint contract="IErroringService" binding="basicHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceProviderBehavior/>
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="serviceProviderBehavior" type="ESBGuidanceErrorHandler.Behaviors.ServiceProviderBehavior, ESBGuidanceErrorHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=de4c0bb04730ca55"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
Add this to all your WCF service providers and any exceptions will be logged and can be shown, filtered and subscribed to in the normal way.