Search code examples
c#visual-studio-2010visual-studio-debugging

VS 2010 - output window - how to enable "find message in code" button?


When I build a Windows Forms project in VS2010 in debug mode (F5) a window appears titled Output Window. There are buttons along the top of the Output Window: "Find Message in Code", "Next Message", and "Previous Message". However, they are always grayed-out (disabled). How do these become enabled?

I'm expecting the intent of these buttons is to help me find lines of code that caused a message to appear in the Output Window. For example, if I write Trace.WriteLine("MyMessage"); in client code, when executed it will make 'MyMessage' appear in the Output Window; I thought by selecting a message in the Output Window and clicking "Find message in Code" it would navigate to the line of the client code that contains "MyMessage". This would be a slick feature if it were enabled and my presumptions were correct. Unfortunately I can't get the buttons to enable.

To answer this question please explain how to enable and use these buttons and if any best practices should be applied (optional).

Here is some source code to use as reference. Create a Windows Forms project and make the changes you see below and you can reproduce what I am attempting.

// program.cs

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace MyNamespace
{
    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                throw new ApplicationException("MyMessage"); 
                Application.Run(new FormMain());
            }
            catch (ApplicationException e)
            {
                Trace.WriteLine(e.ToString());
            }
        }
    }
}

JasonD's recommendation

I replaced

Trace.WriteLine(e.ToString());

with

Trace.WriteLine("Program.cs" + "(" + 23 + ")" + " Some info");

End JasonD solution - result: buttons enabled

That solved it. And what an unexpected answer. In this era of strongly typed everything the answer depends on formatting strings with magic messages. I'm surprised by this.


Solution

  • You need to output something it can parse, which is essentially the same format as error/warning messages you see when compiling, consisting of "FILENAME(LINE) stuff".

    In C#, something like this:

            string file_= new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
            int line_= new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber(); 
            System.Diagnostics.Trace.WriteLine(file_+ "(" + line_.ToString() + ") Some info");
    

    (this is a little messy, but there's no good equivalent of C/C++'s __FILE__ and __LINE__ macros that I've found)

    You can tidy that up a bit and wrap it in a function, but you need to get the file/line of the caller, not the actual function itself:

        static void traceFunc(string msg)
        {
            System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
            string file = trace.GetFrame(1).GetFileName();
            int line = trace.GetFrame(1).GetFileLineNumber();
            System.Diagnostics.Trace.WriteLine(file + "(" + line.ToString() + ") " + msg);
        }