Back in the good old days when learning a new technology was fun because we actually had the time to learn it I picked up a few neat tricks. I started working with VC++ 1.0, pretty much the month it shipped and one of the really cool features was the integrated debugger. From within an MFC application you put a call to a TRACE macro and your message was displayed in the output of the debugger, for vanilla Win32 apps you simply called the OutputDebugString API. More that 10 years later and in my opinion this is still one of the very useful debugging aids the environment offers. One of the little known, is that a cleverly formatted message will enable you to simply double click the message and you are immediately teleported to the source file and line number specified in the message.
For example if the following message was displayed in your debug output window, no this is not a compiler output,
d:\source\myapp\test.cs(23) : Some cool message
Simply by double clicking on the line you will be presented with the source file d:\source\myapp\test.cs and your cursor positioned on line 23.
The following C# function demonstrates a very simplistic implementation, which can be used to log debug messages which automatically include the context information required to easily jump straight into your code by double clicking the message.
public static void TraceEx( string message )
{
// Only log this message if the debugger is attached
if ( System.Diagnostics.Debugger.IsAttached )
{
StackTrace st = new StackTrace(true);
// Get the calling stack frame, the current stack frame is at index 0,
// the caller stack frame is at index 1 and so on.
StackFrame sf = st.GetFrame(1);
if ( sf != null )
{
// Build an appropriately formated message
string str = string.Format( "{0}({1}) : {2}",
sf.GetFileName(), sf.GetFileLineNumber(), message );
// Log the message to the debug window
System.Diagnostics.Trace.WriteLine( str );
}
}
}
If you require, you can also include the column that you would like the cursor positioned at. The following shows this format, take a look at where the line number is specified, it now includes a second value, the column number.
d:\source\myapp\test.cs(23,15) : Some cool message
In this case when you double click the message in the debug window, you will be taken to d:\source\myapp\test.cs line 23, column 15. I have not found much use for this particular formatting, but who knows what you might come up with.