Bagikan melalui


TraceSwitch.Level Properti

Definisi

Mendapatkan atau mengatur tingkat pelacakan yang menentukan pesan yang diizinkan oleh sakelar.

public:
 property System::Diagnostics::TraceLevel Level { System::Diagnostics::TraceLevel get(); void set(System::Diagnostics::TraceLevel value); };
public System.Diagnostics.TraceLevel Level { get; set; }
member this.Level : System.Diagnostics.TraceLevel with get, set
Public Property Level As TraceLevel

Nilai Properti

Salah TraceLevel satu nilai yang menentukan tingkat pesan yang diizinkan oleh pengalih.

Pengecualian

Level diatur ke nilai yang bukan salah TraceLevel satu nilai.

Contoh

Contoh kode berikut membuat baru TraceSwitch dan menggunakan sakelar untuk menentukan apakah akan mencetak pesan kesalahan. Sakelar dibuat di tingkat kelas. MyMethod menulis pesan kesalahan pertama jika Level properti diatur ke TraceLevel.Error atau lebih tinggi. Namun, MyMethod tidak menulis pesan kesalahan kedua jika Level kurang dari TraceLevel.Verbose.

   // Class-level declaration.
   /* Create a TraceSwitch to use in the entire application.*/
private:
   static TraceSwitch^ mySwitch = gcnew TraceSwitch( "mySwitch","Entire Application" );

public:
   static void MyMethod()
   {
      // Write the message if the TraceSwitch level is set to Error or higher.
      if ( mySwitch->TraceError )
            Console::WriteLine( "My error message." );

      // Write the message if the TraceSwitch level is set to Verbose.
      if ( mySwitch->TraceVerbose )
            Console::WriteLine( "My second error message." );
   }

   static void main()
   {
      // Run the method that prints error messages based on the switch level.
      MyMethod();
   }
//Class-level declaration.
/* Create a TraceSwitch to use in the entire application.*/

static TraceSwitch mySwitch = new TraceSwitch("mySwitch", "Entire Application");

static public void MyMethod()
{
    // Write the message if the TraceSwitch level is set to Error or higher.
    if (mySwitch.TraceError)
        Console.WriteLine("My error message.");

    // Write the message if the TraceSwitch level is set to Verbose.
    if (mySwitch.TraceVerbose)
        Console.WriteLine("My second error message.");
}

public static void Main(string[] args)
{
    // Run the method that prints error messages based on the switch level.
    MyMethod();
}
' Class-level declaration.
' Create a TraceSwitch to use in the entire application. 

Private Shared mySwitch As New TraceSwitch("mySwitch", "Entire Application")

Public Shared Sub MyMethod()
    ' Write the message if the TraceSwitch level is set to Error or higher.
    If mySwitch.TraceError Then
        Console.WriteLine("My error message.")
    End If 
    ' Write the message if the TraceSwitch level is set to Verbose.
    If mySwitch.TraceVerbose Then
        Console.WriteLine("My second error message.")
    End If
End Sub

Public Shared Sub Main()
    ' Run the method that prints error messages based on the switch level.
    MyMethod()
End Sub

Keterangan

Untuk .NET Framework aplikasi, untuk mengatur tingkat AndaTraceSwitch, edit file konfigurasi yang sesuai dengan nama aplikasi Anda. Dalam file ini, Anda dapat menambahkan sakelar dan mengatur nilainya, menghapus sakelar, atau menghapus semua sakelar yang sebelumnya diatur oleh aplikasi. File konfigurasi harus diformat seperti contoh berikut:

<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="0" />  
      <add name="myNewSwitch" value="3" />  
      <remove name="mySwitch" />  
      <clear/>  
    </switches>  
  </system.diagnostics>  
</configuration>  

Anda juga dapat menggunakan teks untuk menentukan nilai pengalih. Misalnya, true untuk BooleanSwitch atau teks yang mewakili nilai enumerasi, seperti Error untuk TraceSwitch. Baris <add name="mySwitch" value="Error" /> sama dengan <add name="mySwitch" value="1" />.

Nilai Level default properti adalah TraceLevel.Off. Atau, untuk aplikasi .NET Framework, tingkat diperoleh dari file konfigurasi, jika tersedia.

Mengatur properti ini memperbarui TraceErrorproperti , TraceWarning, TraceInfo, dan TraceVerbose untuk mencerminkan nilai baru.

Berlaku untuk

Lihat juga