I. First, we need to understand the differences between Debug and Trace:
1. What is the difference between Debug. Write and Trace. Write? Which one should I use?
The Debug class provides a set of methods and attributes to help Debug code. The Trace class provides a set of methods and attributes to help track code execution. Generally speaking, it is used to record the execution process of a program without interrupting the program debugging or tracking.
Debug is output only in the debug state, and the Trace is output in the Release state. The Debug content in the Release State disappears.
2. Is there a significant speed change between Debug Build and Release Build? Describe the reason.
First, describe the problem in a table:
Project
Debug
Release
Conditional compilation constant
Debug; Trace
Trace
Code optimization
False
True
Output path
Bin \ Debug
Bin \ Release
Generate debugging information
True
False
The program set generated in Debug mode is the debugging version without optimization. There are two files in the bin \ debug \ directory, except for the .exe or. there is also a. dll file. pdb file, this. the pdb file records debugging information such as breakpoints in the Code. In Release mode, debugging information is not included and the code is optimized. The \ bin \ release \ directory contains only one .exe or. dll file. In addition to bin, the project folder also contains an obj directory. Compilation is compiled by modules. The compilation results of each module are saved in the obj directory. Finally, it is merged into an exe or dll file and saved to the bin. Because each compilation is incremental compilation, that is, only re-Compilation of the changed modules, the function of this obj directory is to save the compilation results of these small blocks and speed up compilation.
Ii. Trace and Bug Sample
Using System;
Using System. Diagnostics; // introduce the namespace of the Debug class
Namespace traceanddebug
{
Class TestDebug
{
Public static void TestDebugMethod ()
{
Debug. Listeners. Add (new TextWriterTraceListener (Console. Out ));
// Direct the Debug class output to the console
Debug. AutoFlush = true;
// Set Debug to automatic output, that is, Flush is called on Listeners after each write.
Debug. Indent ();
// Set indentation
Debug. WriteLine ("Debug WriteLine ()");
// Use Debug to output "Debug WriteLine ()"
Console. WriteLine ("Console. WriteLine ()");
// Use the Console to output "Console. WriteLine ()"
Debug. Unindent ();
// Unindent
// Trace. Listeners. Add (new TextWriterTraceListener (Console. Out ));
// Export the Trace class output to the console
// Trace. AutoFlush = true;
// Set Trace to automatic output, that is, Flush is called on Listeners after each write.
Trace. Indent ();
// Set indentation
Trace. WriteLine ("Trace WriteLine ()");
// Use Trace to output "Trace WriteLine ()"
Console. WriteLine ("Console. WriteLine ()");
// Use the Console to output "Console. WriteLine ()"
Trace. Unindent ();
// Unindent
Console. Read ();
}
}
Class Program
{
Static void Main (string [] args)
{
TestDebug. TestDebugMethod ();
}
}
}
III. C # output Trace and Debug information to the control (for conversion)
The main implementation method is to inherit the TraceLinster class, override the construction parameters, and override the Write and WriteLine methods. The specific code is as follows:
Public class ControlTraceListener: TraceListener
{
Private Control _ control;
Private StringSendDelegate _ invokeWrite;
Private delegate void StringSendDelegate (string msg );
Public ControlTraceListener (Control target)
{
_ Control = target;
_ InvokeWrite = new StringSendDelegate (SendString );
}
Public override void Write (string message)
{
_ Control. Invoke (_ invokeWrite, new object [] {message });
}
Public override void WriteLine (string message)
{
_ Control. Invoke (_ invokeWrite, new object [] {message + Environment. NewLine });
}
Private void SendString (string msg)
{
// No need to lock control as this function will only
// Ever be executed from the UI thread
_ Control. Text + = msg;
}
}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/xufei96/archive/2010/01/13/5186723.aspx