Tracing is a way of monitoring the execution of your application while it is running. .NET offers you two classes for tracing: Trace and Debug (both in System.Diagnostics namespace), they are identical, except that the Debug class functions are not compiled (by default) in release builds.
You can use the Assert method on a Trace/Debug class to evaluate a condition. A standard dialog window will be displayed if the condition is not met.
Trace.Assert(myObject != null, "My object is null!");
If the condition is not met (i.e. when myObject is null) this code will display the following dialog:

The Write and WriteLine methods are used to write output from your application. The output is handled by listeners added to the Trace.TraceListeners collection.
Trace.Listeners.Add(new ConsoleTraceListener()); //add the trace listener.
Trace.WriteLine("This is my message."); //write output
The code above will redirect the trace output to a console window. .NET has a number of other listeners that you can use:
- DefaultTraceListener – this listener is attached by default. It redirects to aVisual Studio output window.
- TextWriterTraceListener – redirects output to a Stream or TextWriter instance. It can be used to write output to files.
- EventLogTraceListener – redirects ouptut to an event log
- ConsoleTraceListener – redirects output to a standard output or standard error stream
- DelimitedListTraceListener – similar to TextWriterListener, but the output is delimited by a selected character (set by Delimiter property)
- XmlWriterTraceListener – redirects XML output to a Stream or TextWriter instance
You can create custom trace listeners by implementing the abstract TraceListener base class. In a recent exchange of emails with Pierre Henri Kuate he suggested that it would be nice to have a trace listener that would write output to AlertGrid. We thought it was a really nice idea and we’ve implemented it (download it here). With the AlertGridTraceListener class a Signal is sent to AlertGrid whenever you call Trace.Write or Trace.WriteLine. The usage is extremely simple. First you initialize the AlertGridTraceListener (you need to pass your AlertGrid api_id)
Trace.Listeners.Add(new AlertGridTraceListener("8a35bcdd-d71e-43e2-9d1c-27394d5e4640"));
Then you use the Write function as so:
Trace.Write(10, "temperature"); //log a name-value pair
Using AlertGrid you can then easily define rules like this:

Imagine your applicaiton logs the temperature value periodically, for instance every 5 minutes. With the AlertGridTraceListener attached you can easily check if the signals are not received and take some notification action automatically:

Finally, you can use AlertGrid to visualise the history of the submitted variables (works for integer or float types)…

This simple example passes only one variable called ‘temperature’. If you want to include a list of name-value pairs in one call, we suggest that you use one of the AlertGrid wrapper classes. The underlying philosophy is identical, but they are a bit more flexible for tracing. Instead of calling Write, you call the SendSignal method on a SignalGenerator instance. You can pass a dictionary of name-value pairs. For example:
/* Trace the weather conditions in Aspen */
IDictionary parameters = new Dictionary();
parameters.Add("temperature", "-5");
parameters.Add("snow", "90.5");
parameters.Add("measurment_time", "01/18/2008 14:34:01");
signalGenerator.SendSignal("Aspen", parameters); //"Aspen" is the name of the AlertGrid receiver object that is to receive the data. You can create multiple receivers for better data organization.
To wrap it all up, with AlertGrid you can easily trace any metric of your application and dispatch alerts (or possibly take any other custom action) whenever something happens (or when it does not happen!). We’ve discussed C# here but AlertGrid can work with any language, we’ve prepared examples and other wrapper classes. See here.
Recent Comments