java debugtrace_Debug与Trace的区别
一.首先要明白Debug與Trace的區(qū)別:
1. Debug.Write和Trace.Write有什么不同?何時(shí)應(yīng)該使用哪一個(gè)?
Debug類提供一組幫助調(diào)試代碼的方法和屬性。Trace類提供一組幫助跟蹤代碼執(zhí)行的方法和屬性,通俗的說就是為在不打斷程序的調(diào)試或跟蹤下,用來記錄程序執(zhí)行的過程。
Debug只在debug狀態(tài)下會(huì)輸出,Trace在Release下也會(huì)輸出,在Release下Debug的內(nèi)容會(huì)消失。
2. Debug Build和Release Build的區(qū)別,是否會(huì)有明顯的速度變化?請(qǐng)說明理由。
首先以一個(gè)表格說明問題:
項(xiàng)目
Debug
Release
條件編譯常數(shù)
Debug;Trace
Trace
優(yōu)化代碼
False
True
輸出路徑
bin\Debug
bin\Release
生成調(diào)試信息
True
False
Debug模式下生成的程序集為調(diào)試版本,未經(jīng)優(yōu)化;在bin\debug\目錄中有兩個(gè)文件,除了要生成的.exe或.dll文件外,還有個(gè).pdb文件,這個(gè).pdb文件中就記錄了代碼中的斷點(diǎn)等調(diào)試信息;Release模式下不包含調(diào)試信息,并對(duì)代碼進(jìn)行了優(yōu)化,\bin\release\目錄下只有一個(gè).exe或.dll文件。在項(xiàng)目文件夾下除了bin外,還有個(gè)obj目錄。編譯是分模塊編譯的,每個(gè)模塊的編譯結(jié)果就保存在了obj目錄下。最后會(huì)合并為一個(gè)exe或者dll文件保存到bin之中。因?yàn)槊看尉幾g都是增量編譯,也就是只重新編譯改變了的模塊,所以這個(gè)obj的目錄的作用就是保存這些小塊的編譯結(jié)果,加快編譯速度。
二.Trace and Bug Sample
using System;
using System.Diagnostics; //引入Debug類所在命名空間
namespace traceanddebug
{
class TestDebug
{
public static void TestDebugMethod()
{
Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
//將Debug類輸出定向到控制臺(tái)輸出
Debug.AutoFlush = true;
//設(shè)置Debug為自動(dòng)輸出,即每次寫入后都調(diào)用Listeners上調(diào)用Flush
Debug.Indent();
//設(shè)置縮進(jìn)
Debug.WriteLine("Debug WriteLine()");
//用Debug輸出"Debug WriteLine()"
Console.WriteLine("Console.WriteLine()");
//用Console輸出"Console.WriteLine()"
Debug.Unindent();
//取消縮進(jìn)
//Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
//將Trace類輸出定向到控制臺(tái)輸出
//Trace.AutoFlush = true;
//設(shè)置Trace為自動(dòng)輸出,即每次寫入后都調(diào)用Listeners上調(diào)用Flush
Trace.Indent();
//設(shè)置縮進(jìn)
Trace.WriteLine("Trace WriteLine()");
//用Trace輸出"Trace WriteLine()"
Console.WriteLine("Console.WriteLine()");
//用Console輸出"Console.WriteLine()"
Trace.Unindent();
//取消縮進(jìn)
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
TestDebug.TestDebugMethod();
}
}
}
三.? C#將Trace,Debug信息輸出到控件上(轉(zhuǎn))
主要實(shí)現(xiàn)方法,繼承TraceLinster類,重寫構(gòu)造參數(shù),重寫Write和WriteLine方法就可以了,具體代碼如下:
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;
}
}
總結(jié)
以上是生活随笔為你收集整理的java debugtrace_Debug与Trace的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: give money是哪首歌啊?
- 下一篇: ke字开头的成语有哪些啊?