共用方式為


逐步解說:變更 My.Application.Log 寫入資訊的位置 (Visual Basic)

您可以使用 My.Application.LogMy.Log 物件來記錄應用程式中發生的事件相關信息。 本操作指南示範如何改寫預設設定,並讓 Log 物件將資料寫入其他日誌接收者。

先決條件

Log 物件可以將資訊寫入數個日誌監聽器。 您需要先判斷記錄檔接聽程式目前的組態,才能變更組態。 如需詳細資訊,請參閱逐步解說 :判斷 My.Application.Log 寫入資訊的位置

您可能想要檢閱 如何:將事件資訊寫入文字檔如何:寫入應用程式事件記錄檔

新增監聽器

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

    (或)

    如果沒有 app.config 檔案:

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

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

    3. 按下 新增

  2. <listeners> 區段中,找出具有 <source> 屬性「DefaultSource」的 name 區段底下的 <sources> 區段。 <sources> 區段位於最上層的 <system.diagnostics> 區段中的 <configuration> 區段。

  3. 將這些元素新增至該 <listeners> 區段。

    <!-- Uncomment to connect the application file log. -->
    <!-- <add name="FileLog" /> -->
    <!-- Uncomment to connect the event log. -->
    <!-- <add name="EventLog" /> -->
    <!-- Uncomment to connect the event log. -->
    <!-- <add name="Delimited" /> -->
    <!-- Uncomment to connect the XML log. -->
    <!-- <add name="XmlWriter" /> -->
    <!-- Uncomment to connect the console log. -->
    <!-- <add name="Console" /> -->
    
  4. 取消註解您要接收 Log 訊息的日誌監聽器。

  5. <sharedListeners> 區段中,在最上層 <system.diagnostics> 區段內,找出 <configuration> 區段。

  6. 將這些元素新增至該 <sharedListeners> 區段。

    <add name="FileLog"
         type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
               Microsoft.VisualBasic, Version=8.0.0.0,
               Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         initializeData="FileLogWriter" />
    <add name="EventLog"
         type="System.Diagnostics.EventLogTraceListener,
               System, Version=2.0.0.0,
               Culture=neutral, PublicKeyToken=b77a5c561934e089"
         initializeData="sample application"/>
    <add name="Delimited"
         type="System.Diagnostics.DelimitedListTraceListener,
               System, Version=2.0.0.0,
               Culture=neutral, PublicKeyToken=b77a5c561934e089"
         initializeData="c:\temp\sampleDelimitedFile.txt"
         traceOutputOptions="DateTime" />
    <add name="XmlWriter"
         type="System.Diagnostics.XmlWriterTraceListener,
               System, Version=2.0.0.0,
               Culture=neutral, PublicKeyToken=b77a5c561934e089"
         initializeData="c:\temp\sampleLogFile.xml" />
    <add name="Console"
         type="System.Diagnostics.ConsoleTraceListener,
               System, Version=2.0.0.0,
               Culture=neutral, PublicKeyToken=b77a5c561934e089"
         initializeData="true" />
    
  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"/>
              <!-- Uncomment to connect the application file log. -->
              <!-- <add name="FileLog" /> -->
              <!-- Uncomment to connect the event log. -->
              <!-- <add name="EventLog" /> -->
              <!-- Uncomment to connect the event log. -->
              <!-- <add name="Delimited" /> -->
              <!-- Uncomment to connect the XML log. -->
              <!-- <add name="XmlWriter" /> -->
              <!-- Uncomment to connect the console log. -->
              <!-- <add name="Console" /> -->
            </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"
               initializeData="FileLogWriter" />
          <add name="EventLog"
               type="System.Diagnostics.EventLogTraceListener,
                     System, Version=2.0.0.0,
                     Culture=neutral, PublicKeyToken=b77a5c561934e089"
               initializeData="sample application"/>
          <add name="Delimited"
               type="System.Diagnostics.DelimitedListTraceListener,
                     System, Version=2.0.0.0,
                     Culture=neutral, PublicKeyToken=b77a5c561934e089"
               initializeData="c:\temp\sampleDelimitedFile.txt"
               traceOutputOptions="DateTime" />
          <add name="XmlWriter"
               type="System.Diagnostics.XmlWriterTraceListener,
                     System, Version=2.0.0.0,
                     Culture=neutral, PublicKeyToken=b77a5c561934e089"
               initializeData="c:\temp\sampleLogFile.xml" />
          <add name="Console"
               type="System.Diagnostics.ConsoleTraceListener,
                     System, Version=2.0.0.0,
                     Culture=neutral, PublicKeyToken=b77a5c561934e089"
               initializeData="true" />
        </sharedListeners>
      </system.diagnostics>
    </configuration>
    

重新設定接聽程式

  1. <add> 區段中找出監聽器的 <sharedListeners> 元素。

  2. 屬性 type 會提供接聽程式類型的名稱。 此類型必須繼承自 TraceListener 類別。 使用強命名型別名稱,確保使用正確的類型。 如需詳細資訊,請參閱下方的「參考強名稱類型」一節。

    您可以使用的一些類型如下:

    要了解其他類型的記錄檔接聽程式寫入資訊的地點,請參閱該類型的文件。

  3. 當應用程式建立記錄接聽程序物件時,它會傳遞 initializeData 屬性作為建構函式參數。 屬性 initializeData 的意義取決於追蹤監聽器。

  4. 建立記錄接聽程式之後,應用程式會設定接聽程序的屬性。 這些屬性是由 元素中的其他 <add> 屬性所定義。 如需特定接聽程式屬性的詳細資訊,請參閱該接聽程式類型的檔。

若要引用強名稱類型

  1. 若要確保日誌接聽器使用正確的類型,請務必使用完整類型名稱和強名稱元件名稱。 強名稱類型的語法如下所示:

    < 類型名稱>、 <元件名稱>、 <版本號碼>、 <文化特性>、 <強名稱>

  2. 此程式碼範例示範如何判斷完全限定類型的強名稱。在此案例中,名稱為 "System.Diagnostics.FileLogTraceListener"。

    Public Sub DisplayStrongName()
        Dim t As Type = GetType(Logging.FileLogTraceListener)
        MsgBox(t.FullName & ", " & t.Assembly.FullName)
    End Sub
    

    這是輸出,而且可用來唯一參考強名稱型別,如上述的「新增接聽程式」程式所示。

    Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

另請參閱