共用方式為


HOW TO:將事件資訊寫入至文字檔

更新:2007 年 11 月

您可以使用 My.Application.Log 和 My.Log 物件,記錄在應用程式中發生的事件資訊。這個範例會顯示如何使用 My.Application.Log.WriteEntry 方法,將追蹤資訊記錄到記錄檔。

若要加入和設定檔案的記錄檔接聽程式

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

    如果沒有 app.config 檔:

    1. 在 [專案] 功能表中,選擇 [加入新項目]。

    2. 從 [加入新項目] 對話方塊中選擇 [應用程式組態檔]。

    3. 按一下 [加入]。

  2. 在應用程式組態檔中尋找 <listeners> 區段。

    您可以利用名稱屬性 (Attribute) "DefaultSource",在 <source> 區段中找到 <listeners> 區段,以巢狀方式放在最上層 <configuration> 區段的 <system.diagnostics> 區段下。

  3. 將這個項目加入至此 <listeners> 區段:

    <add name="FileLogListener" />
    
  4. 在以巢狀方式放在最上層 <configuration> 區段下的 <system.diagnostics> 區段中,尋找 <sharedListeners> 區段。

  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 屬性的值變更為記錄檔目錄。

    注意事項:

    若要設定接聽程式屬性 (Property) 的值,請使用與屬性 (Property) 擁有相同名稱的屬性 (Attribute),而該名稱中的所有字母皆為小寫。例如,location 和 customlocation 屬性 (Attribute) 都會設定 LocationCustomLocation 屬性 (Property) 的值。

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

請參閱

工作

HOW TO:在 Visual Basic 中記錄例外狀況

概念

在 Visual Basic 中使用應用程式記錄檔

參考

My.Application.Log 物件

My.Log 物件

WriteEntry 方法 (My.Application.Log 和 My.Log)

WriteException 方法 (My.Application.Log 和 My.Log)