[Note] use DEBUG and trace in. Net to enhance the debugging capability (samwang)

Source: Internet
Author: User

I recently read the. NET developer debugging strategy. The book is good, but the translation is poor. However, some of the debugging experiences introduced in this article are good and rewarding.

Next I will summarize some of the applications of debug and trace in this book, which is regarded as a kind of Reading Notes.

 

A rough summary of several methods for modifying bugs:

 

  • Debug through the debugger

As its name implies, it is to track the source code for debugging. Microsoft also provides the remote debugging function. Through source code debugging, we generally use the following methods:

  1. Use exception
  2. Use MessageBox.
  3. Use # If debug... # endif
  4. Debug with assertions: both Debug. Assert and trace. Assert are available. debug is only valid in debug mode, while trace is valid. The assertion mode also has an advantage. You can click the button to retry and select the debugger for debugging.
  5. Of course, there are also stack tracing or other special means.

. NET provides powerful asserted debugging. We recommend that you use this method. For specific applications, please find your own materials.

Msdn:

Debug. Assert Method

Trace. Assert Method

 

   

  • Debug logs

You must have encountered some bugs on the client side, but this machine cannot be simulated. The customer needs to be far away from each other, but is not allowed to install remote debugging.

In this case, you can debug the log, that is, write some important information to the text. When a problem or bug occurs, you can view the log file to find the cause.

Recording log files is the operation of writing files. There are many methods.

The listener and switch logs provided by. NET are described here.

1. directly create

1 static void main (string [] ARGs) 2 {3 bool somebool = true; 4 trace. listeners. add (New textwritertracelistener (@ "C: \ mylistener. log "); 5 trace. autoflush = true; // whether to save the logs to the disk after each log write. 6 7 trace. writeline (datetime. now. tostring () + "-- enter function logtest"); 8 trace. indent (); // indent + 1 9 Trace. writeline ("this is indented once"); 10 trace. indent (); 11 trace. writelineif (somebool, "only written if somebool is true"); 12 trace. unindent (); // indent-113 trace. unindent (); 14 trace. writeline ("Leave function logtest"); 15}

 

Log content:

2012-05-30 14:11:41--Enter function LogTest    This is indented once        Only written if someBool is trueLeave function LogTest

 

2. Create a configuration file

App. config

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <system. diagnostics> 4 <trace autoflush = "true" indentsize = "2"> 5 <listeners> 6 <Add name = "mylistener" 7 type = "system. diagnostics. textwritertracelistener "8 initializedata =" C: \ mylistener. log "/> 9 </listeners> 10 </Trace> 11 <switches> 12 <! -- Tracelevel. Off = 0; never record anything! 13 Error = 1; only record error 14 warning = 2; record error and warning 15 info = 3; record error, warning and information 16 verbose = 4; record all content --> 17 <Add name = "myswitch" value = "3"/> 18 <Add name = "yourswitch" value = "4"/> 19 </switches> 20 </system. diagnostics> 21 </configuration>
1 static void main (string [] ARGs) 2 {3 traceswitch Ts = new traceswitch ("myswitch", "descrption"); // switch 4 trace. writeline (datetime. now. tostring () + "-- enter function logtest"); 5 trace. indent (); // indent + 1 6 trace. writeline ("this is indented once"); 7 trace. indent (); 8 trace. writelineif (TS. traceinfo, "myswitch traceinfo is true! "); 9 Trace. unindent (); // indent-110 trace. unindent (); 11 trace. writeline (" Leave function logtest "); 12}

Log content:

2012-05-30 14:19:43--Enter function LogTest  This is indented once    MySwitch TraceInfo is true!Leave function LogTest

 

3. Windows Event Log

You can use the eventlogtracelistener in trace or debug to write Event Logs.

   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.