first, understand the difference between Debug and trace:
1. What is the difference between Debug.Write and trace.write? Which one should be used when.
The Debug class provides a set of methods and properties that help you debug your code. The Trace class provides a set of methods and properties that help track code execution, which is commonly used to record the execution of a program without interrupting the program's debugging or tracking.
Debug is output only in the debug state, and trace will be output under release, and the contents of debug will disappear under release.
2. The difference between the Debug build and the release build will make a noticeable change in speed. Please explain why.
First, explain the problem in a table:
Project
Debug
Release
Conditional Compilation Constants
Debug; Trace
Trace
Optimizing Code
False
True
Output path
Bin\Debug
Bin\release
Generating Debugging information
True
False
The assembly generated in debug mode is a debug version, is not optimized, and has two files in the bin\debug\ directory. In addition to the. exe or. dll file to be generated, there is a. pdb file in which debug information such as breakpoints in the code is recorded, and debug information is not included in release mode, and the code is optimized for \bin\release\ There is only one. exe or. dll file under the directory. There is an obj directory in addition to the bin in the project folder. The compilation is compiled in modules, and the results of each module are stored in the obj directory. Finally, it will be merged into an EXE or DLL file to save to the bin. Because each compilation is an incremental compilation, that is, only the changed module is recompiled, so this obj directory is to save these small blocks of compilation results, speed up the compilation speed.
Two. Trace and Bug Sample
Using System;
Using System.Diagnostics; To introduce the Debug class in the namespace
Namespace Traceanddebug
{
Class Testdebug
{
public static void Testdebugmethod ()
{
DEBUG.LISTENERS.ADD (New TextWriterTraceListener (console.out));
To direct the Debug class output to the console output
Debug.autoflush = true;
Set debug to Automatic output, that is, call listeners on every write flush
Debug.indent ();
Set indent
Debug.WriteLine ("Debug WriteLine ()");
Debug Output "Debug WriteLine ()"
Console.WriteLine ("Console.WriteLine ()");
Output "Console.WriteLine ()" With the Console
Debug.unindent ();
Undo Indent
TRACE.LISTENERS.ADD (New TextWriterTraceListener (console.out));
Directing the trace class output to the console output
Trace.autoflush = true;
Set trace to automatic output, that is, call listeners on every write flush
Trace.indent ();
Set indent
Trace.WriteLine ("Trace WriteLine ()");
Trace Output "Trace WriteLine ()"
Console.WriteLine ("Console.WriteLine ()");
Output "Console.WriteLine ()" With the Console
Trace.unindent ();
Undo Indent
Console.read ();
}
}
Class Program
{
static void Main (string[] args)
{
Testdebug.testdebugmethod ();
}
}
}
Three. C # outputs the Trace,debug information to the control (turn)
The main implementation method, inherits the Tracelinster class, overrides constructs the parameter, overrides the Write and the WriteLine method to be possible, the concrete 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 would only
Ever is executed from the UI thread
_control. Text + msg;
}
}
This article from Csdn Blog, reprinted please indicate the source: http://blog.csdn.net/xufei96/archive/2010/01/13/5186723.aspx