Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Article

A TraceListener that sends messages via UDP

Rate me:
Please Sign up or sign in to vote.
3.18/5 (13 votes)
28 Jul 2004CPOL 63K   17   9
An implementation of a TraceListener that sends messages across the network.

Introduction

Generating application log messages is important for diagnosing and testing a system, and a must for complex and large systems. .NET framework currently provides EventLogTraceListener and TextWriterTraceListener, which work fine for desktop applications; but for more complex server applications which need to be monitored/tested remotely, there is no solution.

Hence, I wrote this small utility NetTraceListener class that extends from TraceListener and outputs log messages to a defined UDP port.

Requirement

You would require a basic UDP notes collector to see the UDP messages.

Code

The entire source file is written in less than 20 lines of code.

The constructor takes a host name (e.g., "127.0.0.1") and a port number, it creates a UDPClient object that is used to send UDP messages.

C#
UdpClient notifier; 
IPEndPoint groupEP; 
public TraceNetListener( string host, int port ) 
{
 groupEP = new IPEndPoint( IPAddress.Parse( host ) ,port );
 notifier = new UdpClient(); 
}

The overridden method Close():

C#
public override void Close() 
{ 
    notifier.Close(); 
    base.Close ();
}

The overridden method Write(), it converts the supplied message into bytes, and sends them over a UDP channel.

C#
public override void Write(string message) 
{
 try 
 {
  byte[] bytes = Encoding.ASCII.GetBytes(message);
  notifier.Send(bytes, bytes.Length, groupEP);
 } 
 catch (Exception e) 
 {} 
}

Now in your main application, you need to add this class as a trace listener.

C#
Trace.Listeners.Add( new TraceNetListener("127.0.0.1", 11000 )); 
Trace.Write("HELLO WORLD !");

And you are all set. When you execute this code, your UDP NoteCollector will get all your trace messages.

License

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


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General"UPD receiver" or "UDP notes collector" Pin
StarCraft28-Apr-07 9:59
StarCraft28-Apr-07 9:59 
GeneralSome thoughts Pin
Lord of Scripts4-Mar-05 23:06
Lord of Scripts4-Mar-05 23:06 
GeneralGreat Idea Pin
Vadim Tabakman12-Jan-05 15:11
Vadim Tabakman12-Jan-05 15:11 
GeneralFile download link is broke Pin
Eric Engler2-Aug-04 9:51
Eric Engler2-Aug-04 9:51 
The file download link is broke.
GeneralTraceTool Pin
Thierry Parent29-Jul-04 19:33
Thierry Parent29-Jul-04 19:33 
GeneralRe: TraceTool Pin
mitchellm4425-Jun-08 8:44
mitchellm4425-Jun-08 8:44 
GeneralProblems Pin
Alois Kraus29-Jul-04 3:49
Alois Kraus29-Jul-04 3:49 
GeneralRe: Problems Pin
Mayank Gupta29-Jul-04 19:39
Mayank Gupta29-Jul-04 19:39 
GeneralRe: Problems Pin
Thomas Lykke Petersen24-Jun-07 22:36
Thomas Lykke Petersen24-Jun-07 22:36 

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

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