Displaying debug messages

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

When developing for iOS, you’ve probably used NSLog to display debugging and status messages. Here's how you would do that with Windows 8 and Windows Phone 8.1.

Writing text to the console / output window

Although Visual Studio provides excellentdebugging tools, there are times when you may want to quickly display a status message to the console. In the same way that iOS provides you with NSLog(), Windows also provides ways to display text on the console.

First, make sure that Visual Studio is displaying the Output window, by selecting Output from the View menu or by pressing CTRL O, W.

To display text in C# use Debug.WriteLine(), like this:

// using System.Diagnostics;
Debug.WriteLine("Hello World");

You can include parameters too, like this:

  // using System.Diagnostics;
string text = "The Answer is:";
int answer = 42;
Debug.WriteLine("Hello. {0} {1}", text, answer);

When using JavaScript, use console.log(), like this:

var text = "The Answer is ";
var answer = 42;
console.log("Hello. ",text,answer);

In C++, the output from OutputDebugString is sent to the Intermediate windows rather than the Output window.

OutputDebugString(L"Hello World\n");

As OutputDebugString only accepts a single string, displaying a numeric value can be a little trickier.

One solution is to define a function to format your data:

void WinLog(const wchar_t *text, int n)
        wchar_t buf[1024];
        _snwprintf_s(buf, 1024, _TRUNCATE, L"%s %d\n", text, n);

This function can then be called like this:

WinLog(L"The Answer is", 42);

Debug.WriteLine Method (String)

JavaScript Console commands

System.Diagnostics namespaces