Debug.Write
This method is found in System.Diagnostics
. It prints a message to the Visual Studio console. It is permanent—it is disabled when Debug is disabled in the program.
We do not need to remove Debug class
calls to improve performance. Other methods in the Debug class
, like WriteLineIf
, are also covered here.
Debug.WriteLine
exampleThe .NET Framework has a Debug static
class
in System.Diagnostics
. Here we see the Write and WriteLine
methods, which may be the most useful overall.
WriteLine
method writes a text line to the "Output" console in Visual Studio.using System.Diagnostics; static class Program { static void Main() { Debug.Write("A"); Debug.Write("B"); Debug.Write("C"); Debug.Write(" "); Debug.WriteLine("Message is written"); } }It will write "ABC Message is written" to the Output window.
Listeners
Next, the Debug type has a Listeners
collection. We add listeners to this. Listeners
are derived from the TraceListener
type.
Listeners
have other names, such as DelimitedListTraceListener
(which writes to a file or stream).DelimitedListTraceListener
is created, with a target file. When Flush is called, the content is written to this file.Listeners
collection gives us a way to output debug information to any target, not just a window in Visual Studio.using System.Diagnostics; class Program { static void Main() { // Create trace listener. TraceListener listener = new DelimitedListTraceListener(@"C:\debugfile.txt"); // Add listener. Debug.Listeners.Add(listener); // Write and flush. Debug.WriteLine("Welcome"); Debug.Flush(); } }Welcome
The Debug type provides the Indent and Unindent methods. These adjust the left-hand margin on the output. This program invokes Debug.Indent
.
Debug.Unindent
. This means the middle two calls to Debug.WriteLine
have an indent level (IndentLevel
) of 1.Debug.IndentLevel
is a property. Instead of using Indent and Unindent, you can assign to IndentLevel
. This is sometimes clearer.using System.Diagnostics; class Program { static void Main() { // 1. Debug.WriteLine("One"); // Indent and then unindent after writing. Debug.Indent(); Debug.WriteLine("Two"); Debug.WriteLine("Three"); Debug.Unindent(); // End. Debug.WriteLine("Four"); // Sleep. System.Threading.Thread.Sleep(10000); } }One Two Three Four
IndentSize
This is the number of spaces an indent has. The program changes IndentSize
to 2, and then directly sets IndentLevel
to 1. This results in an indent of 2 spaces one time.
IndentSize
and IndentLevel
.Debug.Indent
, Unindent, IndentSize
and IndentLevel
members simplify debug output.using System.Diagnostics; class Program { static void Main() { // Write IndentSize. Debug.WriteLine(Debug.IndentSize); // Change IndentSize. Debug.IndentSize = 2; Debug.IndentLevel = 1; Debug.WriteLine("Perls"); // Sleep. System.Threading.Thread.Sleep(10000); } }4 Perls
WriteLineIf
exampleDebug.WriteLineIf
prints output only if a condition is met. Here we demonstrate how Debug.WriteLineIf
and Debug.WriteIf
can be used.
Debug.WriteLineIf
and Debug.WriteIf
have a first argument of bool
type. This can be any expression that evaluates to true or false.IsThursday()
evaluates to true. We use a bool
field and an expression that is evaluated at runtime.using System; using System.Diagnostics; class Program { static void Main() { Debug.WriteLineIf(IsThursday(), "Thursday"); Debug.WriteLineIf(_flag, "Flag"); Debug.WriteLineIf(int.Parse("1") == 1, "One"); Debug.WriteIf(true, "True"); Debug.WriteIf(true, "True"); Debug.WriteIf(false, "False"); Debug.WriteLine("Done"); } static bool IsThursday() { return DateTime.Today.DayOfWeek == DayOfWeek.Thursday; } static bool _flag = true; }
Assert sends a strong message to the developer. An assertion interrupts normal operation of the program but does not terminate the application.
using System; using System.Diagnostics; static class Program { static void Main() { int value = -1; // A. // If value is ever -1, then a dialog will be shown. Debug.Assert(value != -1, "Value must never be -1."); // B. // If you want to only write a line, use WriteLineIf. Debug.WriteLineIf(value == -1, "Value is -1."); } }A. The dialog is displayed. B. Message is written to the Output: Value is -1.
These will be evaluated by the runtime before being converted to a Boolean
value (true or false) and then passed as a value to the methods themselves.
We looked at the Debug.Write
methods. Debug.WriteLine
is the printf
function from C. Permanent error handling with exceptions may be preferable in Release mode compilation.
With TraceListeners
, Debug writes to nearly any source. With expression-based methods, it writes only if a condition matches. And with Assert, it makes errors forcefully known.