Sergey Zhukov
Sergey Zhukov

Reputation: 1372

Output System.Diagnostics.Debug to Console

I want to get output from System.Diagnostigs.Debug in NET Core. Any way is appreciated, but the best I would like to see the output from Debug in Console. I try to use such code:

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

But receive an error:

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

What am I missing? Is it possible to use System.Diagnostics.Debug in .NET Core?

I use netcoreapp1.0 framework.

UPDATE: For Windows I have found a tool DebugView, which shows the output from Debug, https://technet.microsoft.com/en-us/sysinternals/bb896647.aspx, but the question is still actual for Linux and Console output.

Upvotes: 12

Views: 5641

Answers (1)

svick
svick

Reputation: 244797

In .Net Framework, Debug.WriteLine and Trace.WriteLine are the same, the only difference is that Debug.WriteLine is enabled when the DEBUG symbol is set, while Trace.WriteLine is enabled when the TRACE symbol is set.

In .Net Core, Trace.WriteLine still behaves like that, but Debug.WriteLine has a completely different implementation and does not use Listeners, I'm not sure why (you might consider asking at the corefx repo).

The Unix implementation of Debug.WriteLine also has explicit check for the COMPlus_DebugWriteToStdErr envirnment variable, and when its set, it will output Debug.WriteLine to the standard error output.

This means if you use COMPlus_DebugWriteToStdErr=1 dotnet run to run your application, it will work.

If you actually want to use listeners, you will need to use Trace.WriteLine.

Upvotes: 15

Related Questions