逐步解說:篩選 My.Application.Log 輸出 (Visual Basic)

本指南示範如何變更My.Application.Log物件的記錄篩選默認值,以控制從Log物件傳遞至偵聽器的資訊,以及偵聽器寫入的資訊。 即使在建置應用程式之後,您仍可以變更記錄行為,因為組態資訊會儲存在應用程式的組態檔中。

入門指南

寫入的每個訊息 My.Application.Log 都有相關聯的嚴重性層級,其篩選機制會用來控制記錄輸出。 此範例應用程式會使用 My.Application.Log 方法來寫入具有不同嚴重性層級的數個記錄訊息。

建置範例應用程式

  1. 開啟新的 Visual Basic Windows 應用程式專案。

  2. 將名為 Button1 的按鈕新增至 Form1。

  3. 在 Click Button1 的事件處理程式中,新增下列程式代碼:

    ' Activity tracing information
    My.Application.Log.WriteEntry("Entering Button1_Click", TraceEventType.Start)
    
    ' Tracing information
    My.Application.Log.WriteEntry("In Button1_Click", TraceEventType.Information)
    
    ' Create an exception to log.
    Dim ex As New ApplicationException
    ' Exception information
    My.Application.Log.WriteException(ex)
    
    ' Activity tracing information
    My.Application.Log.WriteEntry("Leaving Button1_Click", TraceEventType.Stop)
    
  4. 在調試程式中執行應用程式。

  5. 按 Button1。

    應用程式會將下列資訊寫入應用程式的偵錯輸出和記錄檔。

    DefaultSource Information: 0 : In Button1_Click

    DefaultSource Error: 2 : Error in the application.

  6. 關閉應用程式。

    如需如何檢視應用程式偵錯輸出視窗的資訊,請參閱 輸出視窗。 如需應用程式記錄檔位置的資訊,請參閱逐步解說 :判斷 My.Application.Log 寫入資訊的位置。

    備註

    根據預設,應用程式會在應用程式關閉時排清記錄檔輸出。

    在上述範例中,對方法的第二次呼叫 WriteEntry 和方法的呼叫 WriteException 會產生記錄輸出,而對方法的第一次和最後一次呼叫 WriteEntry 則不會。 這是因為 WriteEntry 和 WriteException 的嚴重性層級分別為「Information」和「Error」,這兩者都被 My.Application.Log 對象的默認日誌篩選所允許。 不過,具有「Start」和「Stop」嚴重性層級的事件被禁止產生記錄輸出。

篩選所有 My.Application.Log 接聽程式

物件 My.Application.Log 使用名為 SourceSwitch 的 DefaultSwitch 來控制從 WriteEntry 和 WriteException 方法傳遞哪些訊息給日誌監聽器。 您可以透過在應用程式的組態檔中,將 DefaultSwitch 的值設定為 SourceLevels 的其中一個列舉值,來進行設定。 根據預設,其值為 「資訊」。

下表顯示在特定的 DefaultSwitch 設定下,日誌記錄將訊息傳送給監聽器所需的嚴重性層級。

DefaultSwitch 值 輸出所需的訊息嚴重性
Critical Critical
Error Critical 或 Error
Warning Critical、 Error或 Warning
Information Critical、Error、Warning或 Information
Verbose Critical、Error、Warning、Information或 Verbose
ActivityTracing Start、Stop、Suspend、Resume或 Transfer
All 允許所有訊息。
Off 所有訊息都會遭到封鎖。

備註

WriteEntry和 WriteException 方法各有一個未指定嚴重性層級的多載。 多載的 WriteEntry 隱含嚴重性層級為「資訊」,而多載的 WriteException 隱含嚴重性層級為「錯誤」。

下表說明了上一個範例中的記錄輸出:設定為預設的 DefaultSwitch "Information" 時,只有對 WriteEntry 方法的第二次呼叫和對 WriteException 方法的呼叫會產生記錄輸出。

僅記錄活動追蹤事件

  1. 以滑鼠右鍵點擊方案總管中的app.config,然後選取開啟。

    -或-

    如果沒有 app.config 檔案:

    1. 在 [ 專案] 功能表上,選擇 [ 新增專案]。

    2. 從 [ 新增專案] 對話框中,選擇 [應用程式組態檔]。

    3. 按下 新增。

  2. 請在最上層 <switches> 區段中的 <system.diagnostics> 區段中找到 <configuration> 區段。

  3. 尋找將 DefaultSwitch 加入開關集合的元素。 它應該看起來類似這個元素:

    <add name="DefaultSwitch" value="Information" />

  4. 將屬性的值 value 變更為 「ActivityTracing」。

  5. app.config 檔案的內容應該類似下列 XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.diagnostics>
        <sources>
          <!-- This section configures My.Application.Log -->
          <source name="DefaultSource" switchName="DefaultSwitch">
            <listeners>
              <add name="FileLog"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="DefaultSwitch" value="ActivityTracing" />
        </switches>
        <sharedListeners>
          <add name="FileLog"
               type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
                     Microsoft.VisualBasic, Version=8.0.0.0,
                     Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
                     processorArchitecture=MSIL"
               initializeData="FileLogWriter"/>
        </sharedListeners>
      </system.diagnostics>
    </configuration>
    
  6. 在調試程式中執行應用程式。

  7. 按 Button1。

    應用程式會將下列資訊寫入應用程式的偵錯輸出和記錄檔:

    DefaultSource Start: 4 : Entering Button1_Click

    DefaultSource Stop: 5 : Leaving Button1_Click

  8. 關閉應用程式。

  9. 將屬性的值 value 變更回 「Information」。

    備註

    DefaultSwitch 開關設定僅控制 My.Application.Log。 它不會變更 .NET System.Diagnostics.Trace 和 System.Diagnostics.Debug 類別的行為。

My.Application.Log 監聽器的單獨篩選

上一個範例示範如何變更所有 My.Application.Log 輸出的篩選。 此範例示範如何篩選個別記錄接聽程式。 根據預設,應用程式有兩個接聽程式會寫入應用程式的偵錯輸出和記錄檔。

設定檔透過允許每個日誌監聽器設有篩選器來控制其行為,這就像是為My.Application.Log添加了一個開關。 只有當記錄檔和記錄接聽程式的篩選器都允許訊息的嚴重性時,記錄接聽程式才會輸出訊息。

此範例示範如何設定新偵錯接聽程序的篩選,並將它新增至 Log 物件。 默認偵錯接聽程式應該從 Log 物件中移除,因此很明顯偵錯訊息來自新的偵錯接聽程式。

僅記錄活動追蹤事件

  1. 按一下滑鼠右鍵 方案總管 中的 app.config,然後選擇 開啟。

    -或-

    如果沒有 app.config 檔案:

    1. 在 [ 專案] 功能表上,選擇 [ 新增專案]。

    2. 從 [ 新增專案] 對話框中,選擇 [應用程式組態檔]。

    3. 按下 新增。

  2. 在方案總管中右鍵單擊 app.config。 選擇 [開啟]。

  3. 在 <listeners> 區段底下的 <source> 區段中,找出具有“DefaultSource”作為 name 屬性的 <sources> 區段。 <sources> 區段位於 <system.diagnostics> 區段之下,並且在最上層 <configuration> 區段中。

  4. 將此元素新增到 <listeners> 區段:

    <!-- Remove the default debug listener. -->
    <remove name="Default"/>
    <!-- Add a filterable debug listener. -->
    <add name="NewDefault"/>
    
  5. 在 <sharedListeners> 區段中,在最上層 <system.diagnostics> 區段內,找出 <configuration> 區段。

  6. 請將此元素新增到該 <sharedListeners> 區段:

    <add name="NewDefault"
         type="System.Diagnostics.DefaultTraceListener,
               System, Version=2.0.0.0, Culture=neutral,
               PublicKeyToken=b77a5c561934e089,
               processorArchitecture=MSIL">
        <filter type="System.Diagnostics.EventTypeFilter"
                initializeData="Error" />
    </add>
    

    篩選條件 EventTypeFilter 會採用其中 SourceLevels 一個列舉值作為其 initializeData 屬性。

  7. app.config 檔案的內容應該類似下列 XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.diagnostics>
        <sources>
          <!-- This section configures My.Application.Log -->
          <source name="DefaultSource" switchName="DefaultSwitch">
            <listeners>
              <add name="FileLog"/>
              <!-- Remove the default debug listener. -->
              <remove name="Default"/>
              <!-- Add a filterable debug listener. -->
              <add name="NewDefault"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
          <add name="FileLog"
               type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
                     Microsoft.VisualBasic, Version=8.0.0.0,
                     Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
                     processorArchitecture=MSIL"
               initializeData="FileLogWriter"/>
          <add name="NewDefault"
               type="System.Diagnostics.DefaultTraceListener,
                     System, Version=2.0.0.0, Culture=neutral,
                     PublicKeyToken=b77a5c561934e089,
                     processorArchitecture=MSIL">
            <filter type="System.Diagnostics.EventTypeFilter"
                    initializeData="Error" />
          </add>
        </sharedListeners>
      </system.diagnostics>
    </configuration>
    
  8. 在調試程式中執行應用程式。

  9. 按 Button1。

    應用程式會將下列資訊寫入至應用程式的記錄檔:

    Default Information: 0 : In Button1_Click

    Default Error: 2 : Error in the application.

    應用程式會將較少的資訊寫入至應用程式的偵錯輸出,因為篩選條件比較嚴格。

    Default Error 2 Error

  10. 關閉應用程式。

如需部署後變更記錄設定的詳細資訊,請參閱 使用應用程式記錄。

另請參閱