Monday, June 15, 2009
We have been using WCF to consume a lot of 3rd party web services, and one of things we depend on when testing is being able to see the actual request and response that is being sent to and from the host. This is expecially inportant in 3rd party services, because the service is essentially a black box in which there is no way to know what is going on except that you send it a request and you get a response back. I posted how, out of the box, you can configure WCF to write the message to a trace xml file, but one of things about writing to a file is that if you or your testers do not have access to the web server where the file is being written then this is not going to work.
The way around this problem is to create a custom trace listener that catches the messages and does what ever you want. Here is a simple sample.
Create a class that inherits from the TraceListiner class.
1 using System.Diagnostics;
3 namespace WcfTrace.Trace
5 public class WebTraceListener : TraceListener
7 public override void Write(string message)
9 //write you custom code here
14 public override void WriteLine(string message)
16 //write your custom code here
The next thing to do is to wire up the custom class in the web.config (or app.config) so that when messages are created ,they caught by the custom trace listener class. The config below is telling source messages to use the sharedListener named xml which the custom trace listener listed above.
110 <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
112 <add name="xml" />
115 <source name="System.ServiceModel.MessageLogging">
117 <add name="xml" />
122 <add name="xml" type="WcfTrace.Trace.WebTraceListener,WcfTrace.Trace" />
That's it. Now when ever a web service call is made, the custom trace listener class is executed, passing it the trace messages.