输出 System.Diagnostics.Debug 到控制台

Output System.Diagnostics.Debug to Console

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

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

但是收到一个错误:

'Debug' does not contain a definition for 'Listeners'

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

我使用netcoreapp1.0框架。

更新: 对于 Windows,我找到了一个工具 DebugView,它显示了 Debughttps://technet.microsoft.com/en-us/sysinternals/bb896647.aspx 的输出,但问题对于 Linux 和 Console 输出仍然是实际的。

在.Net Framework中,Debug.WriteLineTrace.WriteLine是一样的,唯一的区别是设置DEBUG符号时启用Debug.WriteLine,而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