49 C#.DEBUGGING AND TRACING The System.Diagnostics namespace - TopicsExpress



          

49 C#.DEBUGGING AND TRACING The System.Diagnostics namespace includes two classes, debug and trace which you can use to monitor the execution of your application. There are lots of similar methods but there is a difference between both of the classes. Debug statement works only when the program in running the debug mode whereas trace statement works when program is in debug and as well as in the release mode. Both the classes have a method called ASSERT which is used to specify a condition (true or false) together with a format string. If the assert method returns false, it iterrupts the execution and print the message you specified. This method can be used in a long run process where unexpected condition arises. DEMO FOR DEBUG CLASS- int number; Console.WriteLine(Please type a number between 1 and 10, and then press enter); string userInput = Console.ReadLine(); Debug.Assert(int.TryParse(userInput,out number),string.Format(Unable to parse {0} as integer,userInput)); Debug.WriteLine(The current value of userInput is: {0},userInput); Debug.WriteLine(The current value of number is: {0}, number); Console.WriteLine(Press enter to finish); Console.ReadLine(); OUTPUT- TRACE DEMO- string sProdName = Widget; int iUnitQty = 100; double dUnitCost = 1.03; Trace.WriteLine(Trace Information-Product Starting ); Trace.Indent(); Trace.WriteLine(The product name is + sProdName); Trace.WriteLine(The product name is + sProdName, Field); Trace.WriteLineIf(iUnitQty > 50, This message WILL appear); Trace.Assert(dUnitCost > 1, Message will NOT appear); Trace.Unindent(); Trace.WriteLine(Trace Information-Product Ending); Trace.Flush(); OUTPUT-
Posted on: Sun, 19 Jan 2014 11:29:03 +0000

Trending Topics



Recently Viewed Topics




© 2015