Assert and Trace
Assert
System.Diagnostics.Debug is a static class so the Assert() methods may be called directly without object instantiation. The Debug object has a collection of TraceListener's attached such that when Assert() fails it iterates over those TraceListeners sending information about the source of the failure. By default, the Debug object has 1 TraceListener attached, aptly named DefaultTraceListener. The DefaultTraceListen sends its output to two locations. The first is a MessageBox that renders as follows:
Note that the title of the MessageBox explains what happends if you click one of the three available buttons.The second location is OutputDebugString which sends the output to the output windows of any attached debugger as shown below:
Now all of this happens with only a single Assert() method call, no additional application configuration (aka App.config entries) are needed all because of the DefaultTraceLisener. If you aren't running under the debugger as I was when I captured these images, the behavior is the same, but of course you don't see the output window.
Trace
You don't have to use Assert() statements to take advantage of tracing. The Debug class/object also has various Write() and WriteLine() methods that can be utilized to debug an application. Again without any configuration this output is sent to the debuggers output window via OutputDebugString(). If you aren't running the debugger you can use the handy DebugView from Sysinternals. This is handy when you want to view trace output on a computer without Visual Studio installed.
Note that this output will be visible only in debug builds of the application. To see output in non-debug builds, you should use the System.Diagnostics.Trace class/object instead. This class has methods similar to Debug.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace trace_deleteme
{
class Program
{
static void Main(string[] args)
{
Debug.WriteLine("foo");
Trace.WriteLine("bar");
}
}
}If you'd like your trace output to go elsewhere, there are other trace listeners that can be configured. For example to send output to a text file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="file" initializeData="C:\Users\cmedcoff\logs\foobar.log" type="System.Diagnostics.TextWriterTraceListener" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Logging Exceptions
One often wants to trace/log the stack when an exception is thrown. this can be done using all of the tracing/logging mechanisms described here using something link the following:
catch (ArgumentNullException ex)
{
string message = string.Format("{0} caught in {1}\r\n{2}",
ex.GetType().ToString(), new StackFrame().GetMethod().Name, ex.StackTrace);
Util.TraceSource.TraceInformation(message);
Helpful References
- I highly recommend Debugging Applications for Microsoft .NET and Microsoft Windowsby John Robbins