Click here to Skip to main content
Click here to Skip to main content

Creating a real-time Trace listener with WCF

By , 17 Feb 2008
Rate this:
Please Sign up or sign in to vote.

Introduction

This is a trace listener that can be used over a network. I.e., if you have trace messages being written in your application and want to listen to them over the network, you can add a DLL in the bin directory of the app, add a few lines in the config file, and you are ready to listen remotely.

Background

I searched for some similar articles and the above one was close, but needed inclusion into the project being developed. I wanted to be able to have a pristine production service running without anything attached to it in the way of debugging and then, if needed, drop in a DLL and a few config entries and be able to listen to it.

Because of this, I'm not terribly worried about performance. That being said, I used OneWay WCF attributes so it doesn't wait for a response when writing each trace, it just dumps the message and continues.

Using the code

The good news is that in order to use it, you only need code similar to the below in your config file:

<system.diagnostics>
 <trace autoflush="true" indentsize="4">
  <listeners>
   <add name="NetworkTraceListener" 
     type="NetworkTraceListener.TraceClient, NetworkTraceListener"/>
  </listeners>
 </trace>
</system.diagnostics>

<!--<span class="code-comment">WCF config for NetworkTraceListener, pointing to viewer--></span>
<system.serviceModel>
 <client>
   <endpoint 
      address="net.tcp://localhost:8001/NetworkTraceListenerViewer/service" 
      binding="netTcpBinding" contract="ITraceServer" />
 </client>
</system.serviceModel>

Points of interest

You'll need NUnit 4.0 to compile the solution - or remove the test project. To use, launch the WPF app which is the viewer and server, and then include the above and the compiled NetworkTraceListener.dll in the project you want to listen to. You should see the messages in the viewer application.

I used a static Start and Stop method on TraceServer rather than an instance. This was for convenience to get this thing going quickly. If I used an instance class, I would need some way of getting the events from the WCF instance to the separate instance used to start/stop. I didn't foresee needing multiple viewers in the same process, so it seemed the simple way to go.

The TraceClient does something unusual, in that it eats all exceptions. I didn't ever want a communication failure, listener being closed, etc. to cause an app to fail when all I wanted to do was listen to the trace messages. I didn't bother logging those issues because I'm not interested in them and because trying to do additional writing in a Trace listener has caused me some problems in the past.

WCF was very easy to use - I was very happy with how simple it was to setup network functionality without too much wrangling with ports and sockets and what not.

Here is the unit test which confirms that a server can be instantiated, a client can write a couple messages, and they are received:

[Test]
public void TestSingleStartStopWithSendOk()
{
    int MessageCount = 0;
    try
    {
        AutoResetEvent ServerStartWait = new AutoResetEvent(false);
        
        TraceServer.ServiceListening += 
          new TraceServer.ServiceListeningType(delegate(object o, EventArgs e) {
            ServerStartWait.Set();
        });

        TraceServer.Start();
        TraceServer.MessageReceived += 
          new TraceServer.MessageReceivedType(delegate (string Message) {
            MessageCount++;
        });

        //wait for server to start w/ timeout of 20 seconds
        ServerStartWait.WaitOne(20000, true);

        TraceClient client = new TraceClient();
        client.WriteLine("test1");
        client.WriteLine("test2");
        client.Flush();


        //allow time for messages to be sent before forcing connection closed
        Thread.Sleep(1000); 
        client.Close();
    }
    finally
    {
        TraceServer.Stop();
    }
    Assert.AreEqual(2, MessageCount);
}

ToDo

Add a "gratuitous animation" checkbox which enables Star Wars style scrolling trace messages as received. Possible "gratuitous audio" checkbox with theme music.

History

  • 17-Feb-2008 - Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Paul B.

United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Comments and Discussions

 
Questionupdates ? Pinmemberalhambra-eidos30-Mar-11 21:51 
AnswerRe: updates ? PinmemberPaul B.31-Mar-11 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 18 Feb 2008
Article Copyright 2008 by Paul B.
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid