【问题标题】:How to output to console in UWP on Windows 10?如何在 Windows 10 上的 UWP 中输出到控制台?
【发布时间】:2015-12-31 12:28:19
【问题描述】:

有没有办法在 UWP 应用程序中写入控制台/命令提示符/powershell(如Console.WriteLine())或类似内容?

如果控制台不可用,是否有合适的替代方法可以代替我在屏幕上写入大量文本?

我当然可以做一个 XAML 控件并输出到它,但与简单的Console.WriteLine() 相比,它似乎并不方便。

WPF console 上也有非常古老的讨论,但从那里似乎没有任何效果(至少,我找不到Project-Properties-Application tab-Output Type-Console ApplicationTrace.WriteLine("text") 不可用)。

【问题讨论】:

  • 您可以使用 Debug.WriteLine(); 写入输出窗口;

标签: c# xaml console windows-10 uwp


【解决方案1】:

您可以使用 System.Diagnostics 命名空间中的 Debug.WriteLine 方法

MSDN Link

当您开始调试应用程序时,这些消息将显示在输出窗口中(标准 VS 快捷键是 Ctrl+Alt+O,ReSharper 快捷键是 Ctrl+W、O)

【讨论】:

  • 这似乎是部分解决方案,但有没有办法像控制台一样输出到单独的窗口?顺便说一句,至少在 Visual Studio 2015 中,不是输出窗口 Ctrl+Alt+O 吗?
  • 啊,对不起!是的,标准快捷键是 Ctrl+Alt+O,Ctrl+W,O 是 ReSharper 快捷键。关于像控制台这样的单独窗口 - UWP 可以在 Windows Mobile 10 和 Windows 10 上运行。我认为不可能使用控制台运行像单独窗口这样的东西。老实说——我看不出做这样的事情有什么意义。如果它是用于调试目的,那么输出窗口是理想的地方。其他解决方案是加强对显示文本的 XAML 控制(正如您在问题中提到的那样)。
  • 另一个选项是打开新窗口,您可以在其中显示您的日志记录。 Here is example 您应该创建一个新的 XAML 页面(您可以在其中声明一些将显示附加信息的控件)并在新窗口中打开它。但是您必须实现一些自定义逻辑来在窗口之间传输您的信息并显示它。但如果这只是为了调试,那么最好的选择是使用输出窗口。
  • 是否有任何可能的原因导致它在 Visual Studio 2015 更新 2 中不起作用?我在输出窗口中没有看到任何消息。
  • @FeiZheng 它应该可以工作。检查这个 SO question&answer stackoverflow.com/questions/13000507/… 我希望它会对你有所帮助。
【解决方案2】:

从 RS4(2018 年年中发布)开始,您可以使用 UWP 构建命令行应用程序或将信息输出到命令行。预发布 SDK 已经可用,您可以观看Channel 9 video

【讨论】:

    【解决方案3】:

    您可以使用LoggingChannel class。创建ETW 跟踪事件。

    LoggingChannel 很酷的一点是您可以进行复杂的跟踪(并使用高级工具,如PerfView 等),但您也可以使用LoggingChannel.LogMessage Method 来简单地等效于Debug.WriteLine

    public void LogMessage(String eventString)
    

    public void LogMessage(String eventString, LoggingLevel level)
    

    这比Debug.WriteLine 有很多优势:

    • 速度要快得多,您可以轻松记录数百万条消息,而 Debug.WriteLine 速度很慢(基于古老的 Windows 的 OutputDebugString 函数)。
    • 它不会阻止发送者或接收者。
    • 每个频道都由自己的 guid 标识,而使用Debug.WriteLine,您可以从任何地方获取所有跟踪,每个人,找到自己的跟踪有点麻烦。
    • 您可以使用跟踪级别(Critical、Error、Information、Verbose、Warning)
    • 您可以使用 PerfView(如果您真的需要)或 Device Portal 或任何其他 ETW tool

    所以,要发送一些跟踪,只需添加以下内容:

    // 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!");
    ....
    

    现在,如果您想要一种简单的方法在本地计算机上显示这些跟踪,除了使用 PerfView 或其他 ETW 工具之外,您还可以使用我编写的免费开源 GUI 工具 WpfTraceSpy 可用:https://github.com/smourier/TraceSpy#wpftracespy 或这里一个示例 .NET Framework 控制台应用程序,它将所有跟踪及其级别输出到控制台:

    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);
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      我还想补充一点,debug.writeline 似乎在主线程上工作得最好,所以如果使用 async/await,请使用 Device.BeginInvokeOnMainThread(() => Debug.WriteLine(response)); 之类的东西打印到控制台

      【讨论】:

        猜你喜欢
        • 2010-10-09
        • 2016-09-23
        • 2015-11-18
        • 2014-02-28
        • 2019-07-21
        • 1970-01-01
        • 2011-03-09
        • 2010-12-12
        相关资源
        最近更新 更多