Debugging Mono with Trace.WriteLine and Debug.WriteLine
The point of Trace.WriteLine and Debug.WriteLine is to give you the ability to monitor certain areas of your code when debugging or tracing but avoid cluttering your screen or hard drive with useless output when you aren’t. When most people stumble upon Trace/Debug they think it should “Just work”–without any setup–this isn’t the case.
Another great benefits of using Trace/Debug instead of the old Console.WriteLine is you can define exactly where to log to in your app config or with environment variables, so sometimes you might want direct output to the Console but other times you might want to log to a file.
To get your output from Trace/Debug you first need to define either DEBUG or TRACE in your preprocessor directives.
You can do this in code by placing:
#define TRACE
in your code or by passing it at compile time:
gmcs -d:TRACE
after you have your preprocessor directives setup, you have to define where to log to. You can do this by defining it in your app config:
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
or you can define it with the environment variable MONO_TRACE_LISTENER:
export MONO_TRACE_LISTENER=Console.Out
and then you can start your logging with System.Diagnostics.Trace.WriteLine(”Hello World”).
To learn more about Trace/Debug check out:the docs here and here. To learn more about other environment variables you can use with mono ‘man mono’ or click here.








This was just the ticket I needed with the example application configuration file.
Thanks so much for posting this up!
Comment by Peter Smith — August 23, 2008 @ 2:42 pm