Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output System.Diagnostics.Debug to Console

Tags:

.net

.net-core

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.

like image 700
Sergey Zhukov Avatar asked Mar 12 '26 22:03

Sergey Zhukov


1 Answers

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.

like image 73
svick Avatar answered Mar 14 '26 14:03

svick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!