Microsoft already provides a couple great listeners that are great for debugging. The two most commonly used are XmlWriterTraceListener and TextWriterTraceListener, which both dump the diagnostic messages to the file you specify in the configuration options. Microsoft has a great article on how to use these trace listeners for message logging within a WCF application here.
The issue that I ran into was I wanted to log these messages not to the file system, but to the database. Microsoft provides for this in allowing people to create custom Trace Listeners. After some heavy Googling I was able to find this article on MSDN which describes a method in which you would be able to create a custom Trace Listener. Using the code from this article, I was able to boil it down to a simple code shell which anyone could take and use within their WCF application.
This is very helpful for people looking to capture and log the incoming and outgoing SOAP messages to their WCF application without having to create a custom dispatcher. On top of that, creating a Listener provides a drag-and-drop assembly you can use on any future project you might create.
The code for this project can be found here (ZIP, 5.4k). The solution was created using Visual Studio 2005.
Cheers!



#1 by Andreas Schwarz on August 4, 2008 - 4:25 AM
Hi,
i tried to implement your project in a simple WCF-Service.
The only modification i made was commenting out the GetSupportedAttributed()-Method
and adding the following line to TraceEventCore(…
System.Diagnostics.Debug.WriteLine(message);
After modifiying the app.config of my service i get an Exception that the type cannot be loaded:
Eception: Der Typ f?r die Klasse ENusbaum.Examples.CSharp.Listener.Listener konn
te nicht gefunden werden.
bei System.Diagnostics.TraceUtils.GetRuntimeObject(String className, Type bas
eType, String initializeData)
bei System.Diagnostics.TypedElement.BaseGetRuntimeObject()
bei System.Diagnostics.ListenerElement.GetRuntimeObject()
bei System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
bei System.Diagnostics.TraceSource.Initialize()
bei System.Diagnostics.TraceSource.get_Switch()
bei System.ServiceModel.Diagnostics.MessageLogger.InitializeMessageTraceSourc
e()
bei System.ServiceModel.Diagnostics.MessageLogger.EnsureMessageTraceSource()
bei System.ServiceModel.Diagnostics.MessageLogger.set_LogMalformedMessages(Bo
olean value)
bei System.ServiceModel.Diagnostics.MessageLogger.Initialize()
bei System.ServiceModel.Diagnostics.MessageLogger.EnsureInitialized()
bei System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
bei System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
bei System.ServiceModel.Channels.CommunicationObject.Open()
bei MSC.OfferService.Service.Program.Main(String[] arguments) in C:\Developme
nt\Sourcen\Test\OfferService\Program.cs:Zeile 18.
The App.Config looks like this:
…
Could you toss me a small hint what the problem could be ? The default tracing mechanism seems to work fine.
Greetings,
Andreas
#2 by Nirav on December 4, 2009 - 12:48 AM
I have create application for Blackberry Mobile using C#.Net.
please tell me What tool i need?
how can store Database?
How can Create Application?
#3 by Ziyan Gong on January 20, 2010 - 3:30 PM
Hi,
I don’t think the download link works. Could you possibly send the source code to my e-mail? This is exactly what I need to do.
Thanks,
Ziyan
#4 by freggel on November 2, 2010 - 3:51 AM
Hey,
Could you please send me also a copy of your source code, it would help me allot.
Thanks,
Freggel
#5 by Brad on December 21, 2010 - 11:33 AM
I know this is an older post but I would like to see the code that you mentioned above. Thanks
#6 by Angie on December 21, 2011 - 8:29 AM
Hey,
I’m working on doing a WCF service for a big corporation and was wondering if I could take a look at your code to see if I could implement some of your tricks into my code.
Thanks!
Angie