Is there a way to write to console / command prompt / powershell (like Console.WriteLine()
) or anything similar in UWP apps?
If console is unavailable i
Starting with RS4 (the release coming out mid-2018) you can build command - line apps with UWP or output info to the command line. The pre-release SDK is already available and you can watch a Channel 9 video.
You can use the LoggingChannel class. to create ETW trace events.
The cool thing with LoggingChannel is you can do sophisticated traces (and use advanced tools like PerfView, etc.), but you can also have a simple equivalent of Debug.WriteLine
in terms of simplicity with the LoggingChannel.LogMessage Method
public void LogMessage(String eventString)
or
public void LogMessage(String eventString, LoggingLevel level)
This has numerous advantages over Debug.WriteLine
:
Debug.WriteLine
is dog slow (based on the archaic Windows' OutputDebugString
function).Debug.WriteLine
you get all traces from everywhere, everyone, it's a bit messy to find your own ones.So, to send some traces, just add this:
// somewhere in your initialization code, like in `App` constructor
private readonly static LoggingChannel _channel = new LoggingChannel("MyApp",
new LoggingChannelOptions(),
new Guid("01234567-01234-01234-01234-012345678901")); // change this guid, it's yours!
....
// everywhere in your code. add simple string traces like this
_channel.LogMessage("hello from UWP!");
....
Now, if you want a simple way to display those traces on your local machine, beyond using PerfView or other ETW tools, you can use a free open source GUI tool I write called WpfTraceSpy available here: https://github.com/smourier/TraceSpy#wpftracespy or here is a sample .NET Framework Console app that will output all traces and their level to the console:
using System;
using System.Runtime.InteropServices;
using Microsoft.Diagnostics.Tracing; // you need to add the Microsoft.Diagnostics.Tracing.TraceEvent nuget package
using Microsoft.Diagnostics.Tracing.Session;
namespace TraceTest
{
class Program
{
static void Main()
{
// create a real time user mode session
using (var session = new TraceEventSession("MySession"))
{
// use UWP logging channel provider
session.EnableProvider(new Guid("01234567-01234-01234-01234-012345678901")); // use the same guid as for your LoggingChannel
session.Source.AllEvents += Source_AllEvents;
// Set up Ctrl-C to stop the session
Console.CancelKeyPress += (object s, ConsoleCancelEventArgs a) => session.Stop();
session.Source.Process(); // Listen (forever) for events
}
}
private static void Source_AllEvents(TraceEvent obj)
{
// note: this is for the LoggingChannel.LogMessage Method only! you may crash with other providers or methods
var len = (int)(ushort)Marshal.ReadInt16(obj.DataStart);
var stringMessage = Marshal.PtrToStringUni(obj.DataStart + 2, len / 2);
// Output the event text message. You could filter using level.
// TraceEvent also contains a lot of useful informations (timing, process, etc.)
Console.WriteLine(obj.Level + ":" + stringMessage);
}
}
}
You can use Debug.WriteLine method from System.Diagnostics namespace
MSDN Link
When you start debugging your application those messages will be displayed in the Output Window (Standard VS shortcut is Ctrl+Alt+O, ReSharper shortcut is Ctrl+W, O)
I just want to also add that debug.writeline seems to best work on the main thread so if async/await is used use something like Device.BeginInvokeOnMainThread(() => Debug.WriteLine(response));
to print to the console