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.
- <system.diagnostics>
- <trace autoflush="false">
- <listeners>
- <add name="debugListener"
- type="System.Diagnostics.TextWriterTraceListener"
- initializeData="debug.txt" />
- </listeners>
- </trace>
- </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.
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace debugTesting {
- static class Program {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main() {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- System.Diagnostics.Debug.Flush();
- }
- }
- }
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.
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.diagnostics>
- <trace autoflush="true">
- <listeners>
- <add name="debugListener"
- type="System.Diagnostics.TextWriterTraceListener"
- initializeData="debug.txt" />
- </listeners>
- </trace>
- </system.diagnostics>
- </configuration>
0 comments:
Post a Comment