How to build WCF logging

A simple and easy way to log WCF Services is to implement IErrorHandler that allows an implementer to control the fault message returned to the caller and optionally perform custom error processing such as logging …… A : Windows Communication Foundation Logger Framwork So lets create a Project class Library named WcfLogManager. Reference System.ServiceModel.  Next create a ServiceErrorHandlerBehaviorExtensionElement class that inherits from BehaviorExtensionElement . The idea is to configure behavior extensions, which enable the user to customize service or endpoint behaviors. 1 Our next step is to create a ServiceErrorHandler class that inherits from our ServiceErrorHandlerBehaviorExtensionElement and implement IErrorHandler and IServiceBehavior. IServiceBehaviour provides a mechanism to modify or insert custom extensions across an entire service, including the System.ServiceModel.ServiceHostBase. 2 Next implement IErrorHandler Members as folow : 3 Now finish our ServiceErrorHandlerBehaviorExtensionElement implementation as folow : 4 Dont forget to implement IServiceBehavior Members as folow : 5 Next customise your applicationFault class according to your error managment scenario : 6

B : Windows Communication Foundation Test Client

Now its time to test our logger framwork. So lets create a simple WCF application Service client.  Configure the ServiceErrorHandler extension as follow : 7 We must now decore all our operation contracts with the attribute FaultContract of type ApplicationFault 8 9 To test our service, Lets inject an error on our service operation, by throwing a division by Zero 10 Now host our service, by a proxy or another type service host project. and call our operation GetData. You will see when an exception is occured, it will be injected to our ErrorHandler function. So we can log it using any logger framework (Log4Net, EventSource, Unity or other) 11 12 Download sample Project


I'm a microsoft most valuable professional (MVP) .NET Architect and Technical Expert skills located in Paris (FRANCE). The purpose of this blog is mainly to post general .NET tips and tricks, Gora LEYE

Support us

BMC logoBuy me a coffee