Dot Net的调试 - 3

来源:互联网 发布:闲来麻将app源码 编辑:程序博客网 时间:2024/05/23 18:34

 

调试

 

  实际上调试和跟踪用得很普遍。Debug类中的方法有相同的名字的方法,这些方法实现了调试的功能。不同之处是在发布版本配置中是禁止使用的(这意味着不能产生二进制代码调用这些代码)。调试输出也可以在配置文件设置,请看下面:

<confuration>

       <system.diagnostics>

              <debug autoflush = “true” indentsize = “7” / >

       </system.diagnostics>

</confuration>

备注:调试的声明和语法和跟踪很类似。不同之处,就是把有Trace的地方替换为Debug

 

设置调试开关

最后讨论的主题是SwitchSwitch是有一些状态的对象。可以在配置文件或者编程的时候改变状态。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类:TraceSwitchBooleanSwitch。这个例子中我们用使用TraceSwitch依照他们的状态创建输出信息。Switch状态由TraceErrror,TraceInfo,TraceVerboseTraceWarning属性检查。这些属性检查switch状态和如果trace级别等于或大于相应的常量,那么将返回true。例如,当这个级别是2或者更大那么TraceWarningtrue,下面表格是返回值:

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层次。

 

计算性能

这部分我们将告诉你调试的花费时间。事实上,调试对于商业逻辑不起作用。但是调试代码需要花费时间。我们将计算应用程序中输出信息的花费时间。当你测试一个是建要求严格的应用程序时间,测量就很重要。看下面的代码:

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);

                     }

              }

       }

程序测试21000个整数和输出素数。调试的目的是测试每一个输出数字,不管是否是素数。如果数字不是素数,那么输出failed.

对比测量下带调试和不带调试的时间:

 

1

2

3

带调试功能(hh:mm:ss.ff)

000007.9714624

000007.9414192

000007.9714624

不带调试功能

(hh:mm:ss.ff)

000005.1273728

000005.5179344

000005.1273728

可以看出调试是昂贵的例子中花费了64%的执行时间

 

结论:

  文章中描述了调试跟踪.net程序的一般方法。当然还有一些其他问题,如,条件编译我们没有做。想学到更多的东西,可以看msdn。我们希望这篇文章帮助你掌握调试跟踪.net程序的技术。

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝客服不给退货怎么办 天猫客服打字慢怎么办 京东买的kindle坏了怎么办 欧巴怎么办韩语怎么写 聚划算淘宝口令打不开怎么办 道聚城白银礼包下架怎么办 聚星输了很多钱怎么办 弹力运动裤被烟烧了个洞怎么办 生完宝宝胯宽怎么办 黑色纯棉裤子洗的发白怎么办 金盾保险柜密码忘了怎么办 装修好的房子漏水怎么办 刚装修的房子墙面开裂怎么办 刚装修的房子有味道怎么办 代销产品规格填写不完整怎么办 我的信息被泄露怎么办 进入不良网站手机发信息怎么办 发不良信息被停机了怎么办 手机qq登录显示被冻结怎么办 qq账户被冻结了怎么办 qq钱包账户被永久冻结怎么办 怎么办转让费还没给我 收钱吗不能抵用红包怎么办 红包抵扣被关了怎么办 天猫客户给差评怎么办 淘宝闪电退款有纠纷怎么办呀 手机屏幕右下角出现彩色点怎么办 在超市买到假货怎么办 天猫超市多发货怎么办 天猫中不小心取消退款了怎么办 天猫超市写错了怎么办 二维码收付款不到红包怎么办 天猫优惠劵过期了怎么办 天猫购物津贴用不完怎么办 天猫上买的大件东西实物不符怎么办 天猫上面料成分与实物不符怎么办 闲鱼发货与实物不符怎么办 天猫超市买贵了怎么办 天猫超市里购买的东西退货怎么办 淘宝店上传的图片不清楚怎么办 微信图片打印出来不清楚怎么办