共用方式為


如何:將事件資訊寫入文字檔(Visual Basic)

您可以使用 My.Application.LogMy.Log 物件來記錄應用程式中發生的事件相關信息。 此範例示範如何使用 My.Application.Log.WriteEntry 方法將追蹤資訊記錄到記錄檔。

若要新增並設定文件記錄監聽器

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

    (或)

    如果沒有 app.config 檔案:

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

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

    3. 按下 新增

  2. 請在應用程式組態檔中定位<listeners>區段。

    你會在名稱屬性為「DefaultSource」的<listeners>區段中找到<source>區段,該區段位於<system.diagnostics>區段之下,且<system.diagnostics>區段又位於頂層<configuration>區段之下。

  3. 請將此元素新增到該 <listeners> 區段:

    <add name="FileLogListener" />
    
  4. 請在最上層<sharedListeners>區段下的<system.diagnostics>區段中,找到<configuration>區段。

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

    <add name="FileLogListener"
        type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
              Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral,
              PublicKeyToken=b03f5f7f11d50a3a"
        initializeData="FileLogListenerWriter"
        location="Custom"
        customlocation="c:\temp\" />
    

    customlocation屬性的值變更為記錄目錄。

    備註

    若要設定接聽程式屬性的值,請使用與 屬性同名的屬性,並將名稱中的所有字母都放在小寫。 例如,locationcustomlocation 屬性會設定 LocationCustomLocation 屬性的值。

若要將事件資訊寫入檔案記錄檔

使用My.Application.Log.WriteEntryMy.Application.Log.WriteException 方法,將資訊寫入記錄檔。 如需詳細資訊,請參閱 如何:寫入記錄訊息如何:記錄例外狀況

設定元件的檔案記錄檔接聽程序之後,它會接收從該元件寫入的所有訊息 My.Application.Log

另請參閱