Sponsored Ad

Friday, November 27, 2009

C# Testing and Debugging

There are two possible methods

  • Tracepoints
  • Debug class usage

Using trace points cleanest way to test and debug code that work without adding to your code. It is simply appear in the part of own code very similar to a breakpoint.

Debug Class

The Debug class allows you to do what you can do follow-up points, but require actual line of code that is added. However, only add to your code in the development of the construction. The release versions will include no debugging by default.

Add using System.Diagnostics; to the namespaces. Then just reference Debug

Default - By default (C# Express 2005) Debug is disabled but Trace is enabled in the Release build.

Basic

Affirm - Is what I expect? (And the proof is not put in debug release builds)

  • Fail - Use the method Debug.Fail place when you are using the MessageBox.Show statement for 'quick / small experiments.
  • Write, WriteLine - Simple debugging
  • or sangria, sangria - Simple Format
  • WriteIf, WriteLineIf - debugging output depends on outcome

Advanced

When the output window is not enough to place next to turn to external files. There are many ways to write external files. However, many of these segments will leave behind unwanted code in Building launch.

The quickest and easiest way to write a debug file is to add an application configuration file for your project.

Once you have a. Config settings in the project just paste the following code inside the tags.

Code Snippet
  1. <system.diagnostics>
  2.     <trace autoflush="false">
  3.       <listeners>
  4.         <add name="debugListener"
  5.            type="System.Diagnostics.TextWriterTraceListener"
  6.            initializeData="debug.txt" />
  7.       </listeners>
  8.     </trace>
  9.   </system.diagnostics>

If autoflush = "false" must ensure that places a Debug.Flush (); statement before your application closes, otherwise there will be nothing in the file specified. The easiest place to add this is the end of Main () method.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. namespace debugTesting {
  5.   static class Program {
  6.     /// <summary>
  7.     /// The main entry point for the application.
  8.     /// </summary>
  9.     [STAThread]
  10.     static void Main() {
  11.       Application.EnableVisualStyles();
  12.       Application.SetCompatibleTextRenderingDefault(false);
  13.       Application.Run(new Form1());
  14.       System.Diagnostics.Debug.Flush();
  15.     }
  16.   }
  17. }

With autoflush = "false" the program must reach a Debug.Close (); or Debug.Flush (); statement to save debugging information in the file.

It is possible to set autoflush = "true", so if the program fails will have a track record so far. However, this can have a negative impact on the performance of your application, due to the file stream.

Saving debug to file

Save debugging information to a file lets you use your application outside the development environment and still maintain a debug log. To make this a program configuration XML file must be created.

Code Snippet
  1.   <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <system.diagnostics>
  4.     <trace autoflush="true">
  5.       <listeners>
  6.     <add name="debugListener"
  7.            type="System.Diagnostics.TextWriterTraceListener"
  8.            initializeData="debug.txt" />
  9.       </listeners>
  10.     </trace>
  11.   </system.diagnostics>
  12. </configuration>

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers