Dot Net的調試 - 3
2024-07-21 02:23:40
供稿:網友
調試
實際上調試和跟蹤用得很普遍。debug類中的方法有相同的名字的方法,這些方法實現了調試的功能。不同之處是在發布版本配置中是禁止使用的(這意味著不能產生二進制代碼調用這些代碼)。調試輸出也可以在配置文件設置,請看下面:
<confuration>
<system.diagnostics>
<debug autoflush = “true” indentsize = “7” / >
</system.diagnostics>
</confuration>
備注:調試的聲明和語法和跟蹤很類似。不同之處,就是把有trace的地方替換為debug
設置調試開關
最后討論的主題是switch。switch是有一些狀態的對象??梢栽谂渲梦募蛘呔幊痰臅r候改變狀態。switch讓你創建可配置的調試跟蹤代碼。最好了解switch的方法是寫一個段簡單代碼,如下:
using system;
using system.diagnostics;
namespace switching
{
class sampleclass
{
//create a switch. it is initialized by an externally specified value
static traceswitch generalswitch = new traceswitch(“coolswitch”, “global scope”);
static public void samplemethod()
{
//the first message is written if the switch state is set to traceerror
if(generalswitch.traceerror)
console.writeline(“traceerror message”);
//the second message is written if the switch state is set to traceverbose
if (generalswitch.traceverbose)
console.writeline(“traceverbose message”);
//the third message is writeen if the switch state is set to tracewarning
if (generalswitch.tracewarning)
console.writeline(“treacewarning message”);
//the fourth message is written if the switch state is set to traceinfo
if(generalswitch.traceinfo)
console.writeline(“traceinfo message”);
}
public static void main(string[] args)
{
//calls the samplemethod method
samplemethod();
}
}
}
有幾個switch類:traceswitch和booleanswitch。這個例子中我們用使用traceswitch依照他們的狀態創建輸出信息。switch狀態由traceerrror,traceinfo,traceverbose和tracewarning屬性檢查。這些屬性檢查switch狀態和如果trace級別等于或大于相應的常量,那么將返回true。例如,當這個級別是2或者更大那么tracewarning是true,下面表格是返回值:
traceerroe
1
tracewarning
2
traceinfo
3
traceverbose
4
但是,正如我們已經說的,switch的狀態可以在代碼中修改,做個修改代碼的范例:
using system;
using system.diagnostics;
namespace switching
{
class sampleclass
{
//create a switch. it is initialized by an externally specified value
static traceswitch generalswitch = new traceswitch(“coolswitch”, “global scope”);
static public void samplemethod()
{
//the first message is written if the switch state is set to traceerror
if(generalswitch.traceerror)
console.writeline(“traceerror message”);
//the second message is written if the switch state is set to traceverbose
if (generalswitch.traceverbose)
console.writeline(“traceverbose message”);
//the third message is writeen if the switch state is set to tracewarning
if (generalswitch.tracewarning)
console.writeline(“treacewarning message”);
//the fourth message is written if the switch state is set to traceinfo
if(generalswitch.traceinfo)
console.writeline(“traceinfo message”);
}
public static void main(string[] args)
{
console.writeline(“before manual level set/n”);
samplemethod();
generalswitch.level = tracelevel.warning;
samplemethod();
}
}
運行程序,包含以下信息:
before manual level set
traceerror message
tracewarning message
traceinfo message
after manual level set
traceerror message
tracewarning message
這些展示了改變trace switch層次。
計算性能
這部分我們將告訴你調試的花費時間。事實上,調試對于商業邏輯不起作用。但是調試代碼需要花費時間。我們將計算應用程序中輸出信息的花費時間。當你測試一個是建要求嚴格的應用程序時間,測量就很重要??聪旅娴拇a:
using system;
using system.diagnostics;
namespace debugdemo
{
class primenumberdetector
{
public static bool isprime(int n)
{
int upperbound = (int)math.sqrt(n);
for (int i = 2; i <= upperbound; i++)
{
debug.writeline(“processing number” + n + “, testing with “ + i);
if((n%i) == 0)
{
debug.writeline(“failed”);
return false;
}
}
}
public application
{
[stathread]
static void main(string[] args)
{
for(int i = 2; i < 10000;i++)
if (primenumberdetector.isprime(i))
console.writeline(“{0} is prime number” , i);
}
}
}
程序測試2到1000個整數和輸出素數。調試的目的是測試每一個輸出數字,不管是否是素數。如果數字不是素數,那么輸出failed.
對比測量下帶調試和不帶調試的時間:
1
2
3
帶調試功能(hh:mm:ss.ff)
00:00:07.9714624
00:00:07.9414192
00:00:07.9714624
不帶調試功能
(hh:mm:ss.ff)
00:00:05.1273728
00:00:05.5179344
00:00:05.1273728
可以看出調試是昂貴的—例子中花費了64%的執行時間
結論:
文章中描述了調試跟蹤.net程序的一般方法。當然還有一些其他問題,如,條件編譯我們沒有做。想學到更多的東西,可以看msdn。我們希望這篇文章幫助你掌握調試跟蹤.net程序的技術。
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。