C# 에서 Debug Log 사용하기! 요약
C# program that uses Debug.WriteLine
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
C# program that uses DelimitedListTraceListener
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(); } }
Result: debugfile.txt
Welcome
C# program that uses Indent and Unindent
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
C# program that uses IndentSize
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
C# program that demonstrates WriteLineIf
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"); Console.ReadLine(); } static bool IsThursday() { return DateTime.Today.DayOfWeek == DayOfWeek.Thursday; } static bool _flag = true; }
C# program that uses Assert method
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.
Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!