C#中Release和debug模式下调试跟踪程序的原则和方法(二)

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

上接C#中Release和debug模式下调试跟踪程序的原则和方法(一)

实现一个定制的debug类

不幸的是,debug和trace类都不能重写,所以我们只好实现自己的版本,在我们的类中:

trace使用DEBUG属性

assert使用Trace属性

添加了一个警告(warm)功能

添加了一个警告/错误词典。

实现了目前trace/debug的所有功能

可以简单把日志输出的一个文本文件中。

警告和assert,使用debug属性,可以产生一个用户友好的警告信息。

可以抓到未处理的异常,可以进一步提高用户友好特性。

测试版和发布版

在我们的debug类中,作了如下修改:

测试模式(debug mode):

包括所有的跟踪模式(trace mode)的功能,例如:

所有的写功能是打开的。

可以添加接受文件

所有的处理,清理,关闭接受文件的功能是打开的。

Trace mode:

跟踪模式(Trace mode):

可以初始化一个未知的异常

可以输出警告(Warn 方法)

可以打开assert.

未知异常处理

下面的代码是一个未知异常处理器。它提供了一个平和中断程序的方法,作测试模式下(debug mode),可以把异常输出到文件中。

[Conditional("TRACE")]

public static void InitializeUnhandledExceptionHandler()

{

AppDomain.CurrentDomain.UnhandledException+=new UnhandledExceptionEventHandler(DbgExceptionHandler);

}

public static void DbgExceptionHandler(object sender, UnhandledExceptionEventArgs args)

{

Exception e=(Exception) args.ExceptionObject;

Trace.WriteLine("Exception: "+e.Message+"\n"+e.GetType()+"\nStack Trace:\n"+e.StackTrace);

MessageBox.Show(

"A fatal problem has occurred.\n"+e.Message+"\nin: "+e.GetType(),

"Program Stopped",

MessageBoxButtons.OK,

MessageBoxIcon.Stop,

MessageBoxDefaultButton.Button1);

Trace.Close();

Process.GetCurrentProcess().Kill();

}

Warn方法

下面的代码是解释如何给用户输出一个警告信息,提示他们应该作一些什么正确的操作。

[Conditional("TRACE")]

public static void Warn(bool b, DbgKey key)

{

if (!b)

{

Trace.WriteLine("Warning: "+key.Name);

if (problems.Contains(key))

{

string explanation=GetExplanation(key);

MessageBox.Show(

explanation,

"Warning",

MessageBoxButtons.OK,

MessageBoxIcon.Warning,

MessageBoxDefaultButton.Button1);

}

else

{

MessageBox.Show(

"A problem has occurred that should be corrected.\n\nReference: "+key.Name,

"Warning",

MessageBoxButtons.OK,

MessageBoxIcon.Warning,

MessageBoxDefaultButton.Button1);

}

}

}

Assert 方法

当调用一个重写的Assert(bool b, DbgKey key)方法时,可以产生一个断言信息,并提示给用户一个友好的信息。这个方法和上面的warn方法,可以使用错误字典来给用户显示一些有用的信息。

[Conditional("TRACE")]

public static void Assert(bool b, DbgKey key)

{

if (!b)

{

Trace.WriteLine("Assert: "+key.Name);

if (problems.Contains(key))

{

string explanation=GetExplanation(key);

MessageBox.Show(

explanation,

"Program Stopped",

MessageBoxButtons.OK,

MessageBoxIcon.Stop,

MessageBoxDefaultButton.Button1);

}

else

{

MessageBox.Show(

"A fatal problem has occurred.\n\nReference: "+key.Name,

"Program Stopped",

MessageBoxButtons.OK,

MessageBoxIcon.Stop,

MessageBoxDefaultButton.Button1);

}

Trace.Close();

Process.GetCurrentProcess().Kill();

}

}

Verify方法

public static void Verify(bool b)

{

Assert(b, new DbgKey("_NEVER_FAIL_"));

}

添加到日志文件中

调用LogOutput,可以添加到日志文件中。

[Conditional("DEBUG")]

public static void LogOutput(string fn)

{

Debug.Listeners.Add(new TextWriterTraceListener(fn));

DateTime dt=DateTime.Now;

Debug.WriteLine("Debug Logging Initialized: "+dt.ToString());

}

使用方法

下面的例子显示如何使用我们的写好类

Dbg.LogOutput("out.txt");

Dbg.InitializeUnhandledExceptionHandler();

Dbg.Problems.Add(new DbgKey("IO"),

"Cannot create file.",

new String[] {

"The specified path is invalid.",

"The network connection is down.",

"The file already exists."});

Dbg.Warn(false, new DbgKey("IO"));

Dbg.Warn(false, new DbgKey("Foo"));

Dbg.WriteLine("A trace message");

int a=0;

int n=1/a; // generate an unhandled exception

Dbg.Assert(false, new DbgKey("IO")); // comment out above exception to make this happen

结束语

在上面的文章中,我们知道了在测试模式下(debug mdoe),assert和trace都是应该打开的。在发布模式下(release mode),只有assert是打开的。接着,我们知道了如果不能在try-catch中处理的异常,就应该是assert,如果能处理的就要显示给用户一个警告信息。最后,我们看到assert应该提供一些问题如何解决的方案给我们的用户。同时,在测试状态中的版本,所有的trace和assert都要写到日志文件中,而在发布版本中,就没有日志文件了。

当然,如果能扩展这个类使他能从XML文件中读取错误字典,就更好了。

参考资料

Writing Custom .NET Trace Listeners, Vagif Abilov (CodeProject)

General Guidelines for C# Class Implementation, Eddie Velasquez (CodeProject)

补充:

代码下载:http://www.codeproject.com/useritems/DebugTreatise/DebugTreatise_src.zip

Debug/Trace 金字塔图

(全文完)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航