|
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.
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.