<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      Dot Net的調試 - 3

      [摘要]調試 實際上調試和跟蹤用得很普遍。Debug類中的方法有相同的名字的方法,這些方法實現了調試的功能。不同之處是在發布版本配置中是禁止使用的(這意味著不能產生二進制代碼調用這些代碼)。調試輸出也可...
      調試



      實際上調試和跟蹤用得很普遍。Debug類中的方法有相同的名字的方法,這些方法實現了調試的功能。不同之處是在發布版本配置中是禁止使用的(這意味著不能產生二進制代碼調用這些代碼)。調試輸出也可以在配置文件設置,請看下面:

      <confuration>

      <system.diagnostics>

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

      </system.diagnostics>

      </confuration>

      備注:調試的聲明和語法和跟蹤很類似。不同之處,就是把有Trace的地方替換為Debug



      設置調試開關

      最后討論的主題是Switch。Switch是有一些狀態的對象。可以在配置文件或者編程的時候改變狀態。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程序的技術。





      日韩精品一区二区三区高清