views:

160

answers:

4

I have seen several projects that use the Trace functionality to capture events and stream them out to a log file. I have been unsuccessful in finding a simple to follow guide that will show me how to configure Trace to capture and write said logfile. Does anyone have a link recommendations, or provide some simple steps to follow?

A: 

Take a look at logging frameworks. We rolled out own, but are now migrating over to log4net available free at http://logging.apache.org/log4net/

Simon Hughes
+2  A: 

The Trace object writes the statements to any attached TraceListeners. You can build your own, but there are a number already defined in the System.Diagnostics namespace, including:

  • ConsoleTraceListener (Console)
  • DefaultTraceListener (Visual Studio / Debugger)
  • DelimitedListTraceListener (TextWriter, special formatting)
  • EventLogTraceListener (EventLog - anything that inherits from System.Diagnostics.EventLog)
  • TextWriterTraceListener (TextWriter - think file)

You can, of course, inherit your own from the TraceListener class that writes to where ever you want. For example, you could log to a database, have it send e-mails or pages in certain situations, or write the statements back to a logging platform like log4net.

The big thing is that you need to create an instance of whatever listeners you want and then add them to the Trace' class Listeners collection. You can add as many as you need and Trace will write to all of them. This way, you can write your logging code once using a well-supported and understood object that's part of the framework, and you can attach anything you need to it.

Joel Coehoorn
A: 

I stumbled into a MSDN article that really helps. Sorry I didn't find it before posting the question but perhaps others may have the same question and haven't found this link.

Dscoduc
This is a very old article, it is from 2001, but it is still relevant.
BrokeMyLegBiking
A: 

Im looking for a way to set the Category of the EventLog, the FormattedEventLogTraceListener writes into (not the category of the message).

But I can't find an appropriate property of this class.

Is it possible to set this?