« PreviousNext »

Creating a Custom Listener for your WCF application in C#

19 May 2007

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!

Posted in C# Programming, General Programming, Microsoft .NET 3.0 / WinFX | Trackback | del.icio.us | Top Of Page

    One Response to “Creating a Custom Listener for your WCF application in C#”

  1. Andreas Schwarz Says:

    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

Leave a Reply


Powered by WP Hashcash