16

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?

AkselK
  • 2,563
  • 21
  • 39

2 Answers2

17

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.

Community
  • 1
  • 1
AkselK
  • 2,563
  • 21
  • 39
  • 1
    In order to add a message after the line number (you probably do) you must add a colon. It would look like this: `string.Format("{0}({1}): {2}", filePath, lineNumber, msg);` – Sébastien Duval Jun 11 '14 at 16:30
  • 2
    Official article: [Formatting the Output of a Custom Build Step or Build Event](https://learn.microsoft.com/en-us/cpp/ide/formatting-the-output-of-a-custom-build-step-or-build-event) – Helder Magalhães Aug 04 '17 at 16:15
  • 1
    Blog entry: [MSBuild / Visual Studio aware error messages and message formats](https://blogs.msdn.microsoft.com/msbuild/2006/11/02/msbuild-visual-studio-aware-error-messages-and-message-formats/) (not as complete/official as previous link but a bit more descriptive). – Helder Magalhães Aug 06 '17 at 11:55
  • +1, but I'm unable to get the output coloring bit at the end to work, nor can I find any documentation on this particular feature. (I've tried various whitespace combinations, but no luck.) I have found extensions that do this, but simply being able to add " : error" to make things stand out would be nice. – Dan Jun 16 '18 at 20:02
5

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.

aremmell
  • 1,151
  • 1
  • 10
  • 15