/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; using System.IO; using System.Diagnostics; namespace Client.Chapter_16___Debugging { public class Class1Chapter_16___Debugging1 { [STAThread] static void Main(string[] args) { Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Trace.WriteLine("My Trace to the console"); } } }
Author: coder
Trace to debuger: writeline and flush
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; using System.IO; using System.Diagnostics; namespace Client.Chapter_16___Debugging { public class TracingToDebugger { [STAThread] static void Main(string[] args) { Trace.WriteLine("My Trace String"); Trace.Flush(); } } }
Trace to event log
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; using System.IO; using System.Diagnostics; namespace Client.Chapter_16___Debugging { public class TracingToEventLog { [STAThread] static void Main(string[] args) { //You can change the listener with the following code EventLogTraceListener EventListener = new EventLogTraceListener("MyApp"); Trace.Listeners.Add(EventListener); Trace.WriteLine("My Trace String To Console"); } } }
Debug class
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; using System.IO; using System.Diagnostics; namespace Client.Chapter_16___Debugging { public class Class1Chapter_16___Debugging { [STAThread] static void Main(string[] args) { int i = 0; Trace.Assert((i == 1), "My Trace Assertion"); Debug.Assert((i == 1), "My Debug Assertion"); } } }
use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and AddSeconds() methods to add periods to a DateTime
using System; class MainClass { public static void Main() { DateTime myDateTime9 = new DateTime(2004, 1, 1); Console.WriteLine("Initial myDateTime9 = " + myDateTime9); myDateTime9 = myDateTime9.AddYears(1); myDateTime9 = myDateTime9.AddMonths(5); myDateTime9 = myDateTime9.AddDays(3); myDateTime9 = myDateTime9.AddMinutes(30); myDateTime9 = myDateTime9.AddSeconds(15); Console.WriteLine("Final myDateTime9 = " + myDateTime9); } }
use the overloaded subtraction operator (-) to subtract a TimeSpan from a DateTime
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10); DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30"); DateTime myDateTime8 = myDateTime6 - myTimeSpan; Console.WriteLine("myDateTime6 - myTimeSpan = " + myDateTime8); } }
use the overloaded addition operator (+) to add a TimeSpan to a DateTime
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10); DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30"); DateTime myDateTime8 = myDateTime6 + myTimeSpan; Console.WriteLine("myDateTime6 + myTimeSpan = " + myDateTime8); } }