Debug and Trace Classes in .Net #18

Debug and Trace classes in .Net

You can always debug your applications line by line. However, when the applications get very bigger/larger, debugging line by line may not be an efficient way for finding errors in code. Debug and Trace classes are handy in such cases. Using Debug and Trace classes, you can find errors and write them in a file or create an event log. Later, you can check the file or the event log to see what kinds of errors the applications are causing. They both provide the same functionalities except that Debug class works only in the debug mode. Trace can work even after the applications are released and also you can control the error reporting from released versions. In a .config file you can change the behaviour of the Trace class.

Logging Trace Output into a text file

  • 1. Create a FileStream object instance and map it to a file
  • 2. Create a TextWriterTraceListener instance and make the text file as the targe of this listener
  • 3. Add this listener to the Trace.Listeners collection
  • 4. set Trace.AutoFlush to be true
  • 5. where required you can use Trace.Write method to send error messages to the file
  • 6. Trace.Write, Trace.WriteLine, Trace.WriteIf, Trace.WriteLineIf can be used to send error message to the file
  • 7. Trace.Assert method writes error messages only if the supplied condition fails. Trace.Assert also displays the error in a MessageBox
  • 8. Trace.Fail causes an Trace.Assert unconditionally

Logging Trace outout into an Event Log

  • 1. Create or open an EventLog
  • 2. Set the Source property of Event Log
  • 3. Create an instance of EventLogTraceWriter and specify the new event log as it’s target
  • 4. Use Trace.AutoFlush[automatic sending], or Trace.Flush [send error messages explicitly]
  • 5. Where required, you can use Trace.Write method to send error messages to the file
  • 6. Trace.Write, Trace.WriteLine, Trace.WriteIf, Trace.WriteLineIf can be used to send error messages to the file
  • 7. Trace.Assert method writes error messages only if the supplied condition fails. Trace.Assert also displays the errors in a MessageBox
  • 8. Trace.Fail causes a Trace.Assert unconditionally

TraceSwitches

TraceSwitch/BooleanSwitch class can be used to control the error messaging that the Trace class generates.

From: http://sitestree.com/?p=4758
Categories:18
Tags:
Post Data:2008-09-26 11:22:29

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>