【问题标题】:Output System.Diagnostics.Debug to Console输出 System.Diagnostics.Debug 到控制台
【发布时间】:2016-06-17 15:45:59
【问题描述】:

我想从 NET Core 中的 System.Diagnostigs.Debug 获取输出。任何方式都值得赞赏,但最好是我希望在控制台中看到Debug 的输出。我尝试使用这样的代码:

TextWriterTraceListener myWriter = new 
   TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);

但是收到错误:

“调试”不包含“侦听器”的定义

我错过了什么? .NET Core 中是否可以使用System.Diagnostics.Debug?

我使用netcoreapp1.0 框架。

更新: 对于 Windows,我找到了一个工具 DebugView,它显示了来自 Debug、https://technet.microsoft.com/en-us/sysinternals/bb896647.aspx 的输出,但对于 Linux 和 Console 输出,问题仍然存在。

【问题讨论】:

    标签: .net .net-core


    【解决方案1】:

    在.Net Framework中,Debug.WriteLine和Trace.WriteLine是一样的,唯一的区别是Debug.WriteLine是在设置DEBUG符号时启用,而Trace.WriteLine是在TRACE符号时启用设置好了。

    在 .Net Core 中,Trace.WriteLine 的行为仍然如此,但Debug.WriteLine 的实现完全不同,并且不使用Listeners,我不知道为什么(您可以考虑在the corefx repo 询问)。

    The Unix implementation of Debug.WriteLine 还对COMPlus_DebugWriteToStdErr 环境变量进行了显式检查,当它被设置时,它会将Debug.WriteLine 输出到标准错误输出。

    这意味着如果您使用COMPlus_DebugWriteToStdErr=1 dotnet run 来运行您的应用程序,它将工作。

    如果你真的想使用监听器,你需要使用Trace.WriteLine。

    【讨论】:

    • 你好。 Visual Studio 新手在这里。 “Debug.WriteLine 在设置DEBUG 符号时启用”...如何设置DEBUG 符号?
    • @ocramot 默认在调试模式下设置。您可以在项目属性中更改它。
    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多