So far in my application i am logging all the trace events to a file on my hard drive.
I now want to enhance it in the form of a separate windows trace application which listens
to the trace from the Main application (as they are generated) and report it on a
gridview like interface. Now the question is :
What kind of TraceListener i have to use to get a maximum benefit interms of speed at which the log information is read ?
Restrictions Owing to certain restrictions, i cannot use the database logging and reading approach
Will listening to the application eventLogs help in any way?
Thanks for the suggestions and time.
The default trace listener will use the win32 api OutputDebugString. You can listen to messages passed to this method using existing tools. Have a look at this, for instance:
http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
Maybe that will save you the time it takes to write your own.
If you do want to write your own and your main concern is to get trace messages as fast as possible to the trace viewer application, then you could have your TraceListener accept network connections from the viewer. Whenever a trace messages is handled by the trace listener, you would write to the network. If you're not concerned about being able to view trace messages on a remote machine, then listening to whats put to OutputDebugString is also an option, of course.
This would effect the performance of the application doing the tracing of course so writing to the network is best done asynchronously without blocking the trace write call. While writing to the network you would have to add trace messages to a queue to process.
Here is a simple example which probably works:
public class NetworkPublishingTraceListener : TraceListener {
private List<string> messages = new List<string>();
private AutoResetEvent messagesAvailable = new AutoResetEvent(false);
private List<TcpClient> traceViewerApps;
private object messageQueueLock = new object();
public NetworkPublishingTraceListener(int port) {
// Setup code for accepting and dealing with network connections.
(new Thread(BackgroundThread) { IsBackground = true }).Start();
}
public override void Write(string message) {
if (traceViewerApps.Count == 0) {
return;
}
lock (messageQueueLock) {
messages.Add(message);
}
messagesAvailable.Set();
}
public override void WriteLine(string message) {
Write(message + Environment.NewLine);
}
private void BackgroundThread() {
while (true) {
messagesAvailable.WaitOne();
List<string> messagesToWrite;
lock (messageQueueLock) {
messagesToWrite = messages;
messages = new List<string>();
}
traceViewerApps.ForEach(viewerApp => {
StreamWriter writer = new StreamWriter(viewerApp.GetStream());
messagesToWrite.ForEach(message => writer.Write(message));
});
}
}
}