Click here to Skip to main content
15,881,898 members
Articles / Desktop Programming / WPF

Creating a real-time Trace listener with WCF

Rate me:
Please Sign up or sign in to vote.
3.21/5 (10 votes)
17 Feb 2008CPOL2 min read 40.8K   592   23  
Instead of reading trace messages in a file, get them in a WPF app real-time via WCF.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace NetworkTraceListener
{
    /// <summary>
    /// The Trace Listener client. It will listen to the app for trace messages and then send them over the network to a viewer/server based on config file settings in app.
    /// </summary>
    public class TraceClient : TraceListener
    {
        public override void WriteLine(string message)
        {
            Write(message + Environment.NewLine);
        }

        public override void Write(string message)
        {
            MessagesTraced++;

            //only send if opened and ready, ignore errors in case server closed
            //after some successful sends have occurred
            try
            {
                if(proxy.State == System.ServiceModel.CommunicationState.Opened)
                    proxy.TraceMessage(message);
            }
            catch (Exception) { }
        }

        /// <summary>
        /// Used for testing or just determining how many messages have been traced thus far
        /// </summary>
        public int MessagesTraced { get; set; }

        TraceServerClient proxy = null;

        public TraceClient() : this("") { }
        public TraceClient(string name) : base(name)
        {
            //if server isn't there don't want to throw an error
            //so that if a viewer isn't listening the app using
            //this client doesn't throw an error
            try
            {
                proxy = new TraceServerClient();
                proxy.Open();
            }
            catch (Exception) { }
        }

        ~TraceClient()
        {
            //if server isn't there don't want to throw an error
            //so that if a viewer isn't listening the app using
            //this client doesn't throw an error
            try
            {
                if (proxy != null)
                    proxy.Close();
            }
            catch (Exception) { }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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