Uniting WCF Tracing with the Unity Application Block
Background
An application in the presentation tier sends a request to a WCF service. With “includeExceptionDetailInFaults” at the WCF service set to false (which is the recommended setting for production environments), should an exception occur in the WCF service, the exception that will be raised in the client application will be along the lines of “the server was unable to process the request due to an internal error”.
Assuming that exceptions are logged by both the client and the service, the challenge is to facilitate troubleshooting by being able to link error messages from one exception log to another. In particular, the details that would be useful are:
- What method in which client caused the exception in the WCF service?
- What were the parameters sent to the service?
- The complete stack trace, from the beginning – typically a user action at the client – to the end – the method in the service throwing the exception.
WCF Tracing
WCF provides application instrumentation and diagnostic data for fault monitoring and analysis. Tracing can be used instead of a debugger to understand how an application is behaving, or why it faults. In particular, faults and processing across components can be correlated to provide end-to-end troubleshooting. This correlation is enabled by the propagation of an activity ID in the message headers:
“Propagation provides the user with direct correlation of error traces for the same unit of processing across application endpoints, e.g., a request. Errors emitted at different endpoints for the same unit of processing are grouped in the same activity, even across application domains. This is done through propagation of the activity ID in the message headers. Therefore, if a client times out because of an internal error in the server, both errors appear in the same activity for direct correlation.” (MSDN - WCF End-to-End Tracing - Propagation)
Integration with Unity
Your application might already implement interception and policy injection, provided by the Unity Application Block. The advantage of interception and policy injection is that business logic is not cluttered with code to handle “infrastructure” requirements such as validation and logging.
Integration of WCF tracing with Unity is quite straightforward as WCF tracing is able to be implemented either via configuration or programmatically. Thus, all that is needed is a custom call handler for interception. This call handler will then programmatically implement WCF tracing. Thus WCF tracing is able to be finetuned by the usage of matching rules.
Comments: