Double click to go to source in Output Window

2019-04-19 16:28发布

问题:

When you build a project in Visual Studio, the Output Window outputs the status of the build process, which included errors and warnings. Double clicking those lines will open up the file containing that error/warning in the editor.

Now, is it possible to get that functionality with output from Debug.WriteLine, or anything like that? So that when the Debug window outputs for example

Buffering: 13:03:20 to 13:03:21

I would be able to double click it and be taken to BufferClass.cs, line 45, since that was the location of the Debug.WriteLine call.

Is that possible, either through .net libraries, or through a Visual Studio Extension?

回答1:

I'll just go ahead and answer this by myself then.

To be able to directly jump to the source file, format your message like this:

string.Format("{0}({1})", filePath, lineNumber);

That way, Visual Studio will automatically add the double click functionality and take you directly to the source.

Additionally, if you use the new functionality in Visual Studio 2012, as described here: Caller Details, you can implement your Log Method like this:

private void LogData(string message, 
                     [CallerMemberName] string callerName = "",
                     [CallerLineNumber] int lineNumber = -1,
                     [CallerFilePath] string filePath = "")
    {
        Debug.WriteLine(message);
        Debug.WriteLine(string.Format("    {0}({1})", filePath, lineNumber));
    }

In addition, adding " : error" or ": warning" to the end makes Visual Studio color it red or yellow. If there are any articles describing this further, I'd really like to know.



回答2:

Well, this question (and answer) appear to be a bit outdated, so let me refresh:

In Visual Studio 2013, the following format is the only one that appears to cause linkage to the file/line that echoed the message:

C#:

{0}({1}): <message here>

For C/C++, give this a whirl:

#define STRINGX(x) #x
#define STRING(x) STRINGX(x)
#define MY_LOG(msg) __pragma(message(__FILE__"(" STRING(__LINE__) "): " msg))

If you don't include the colon after the ending parenthesis, or there is a space between the file name and line number, it will not link to the source code.