作法:建立和初始化追蹤來源
注意
本文專屬於 .NET Framework。 其不適用於較新的 .NET 實作,包括 .NET 6 和更新版本。
應用程式會使用 TraceSource 類別產生能夠與應用程式相關聯的追蹤。 TraceSource 提供了追蹤方法,能讓您輕鬆地追蹤事件、追蹤資料和問題資訊追蹤。 不論是否使用組態檔,都可以從 TraceSource 建立及初始化追蹤輸出。 本主題提供這兩個選項的指示。 不過,建議您使用組態檔來協助重新設定追蹤來源於執行階段所產生的追蹤。
若要使用組態檔建立及初始化追蹤來源
建立 Visual Studio 主控台應用程式專案 (.NET Framework),並且將提供的程式碼取代為下列程式碼。 這個程式碼會記錄錯誤和警告,並且將其中一部分輸出至主控台,而另一部分輸出至 myListener 檔 (該檔案是由組態檔中的項目所建立)。
using System; using System.Diagnostics; class TraceTest { private static TraceSource mySource = new TraceSource("TraceSourceApp"); static void Main(string[] args) { // Issue an error and a warning message. Only the error message // should be logged. Activity1(); // Save the original settings from the configuration file. EventTypeFilter configFilter = (EventTypeFilter)mySource.Listeners["console"].Filter; // Create a new event type filter that ensures // warning messages will be written. mySource.Listeners["console"].Filter = new EventTypeFilter(SourceLevels.Warning); // Allow the trace source to send messages to listeners // for all event types. This statement will override // any settings in the configuration file. // If you do not change the switch level, the event filter // changes have no effect. mySource.Switch.Level = SourceLevels.All; // Issue a warning and a critical message. Both should be logged. Activity2(); // Restore the original filter settings. mySource.Listeners["console"].Filter = configFilter; Activity3(); mySource.Close(); return; } static void Activity1() { mySource.TraceEvent(TraceEventType.Error, 1, "Error message."); mySource.TraceEvent(TraceEventType.Warning, 2, "Warning message."); } static void Activity2() { mySource.TraceEvent(TraceEventType.Critical, 3, "Critical message."); mySource.TraceEvent(TraceEventType.Warning, 2, "Warning message."); } static void Activity3() { mySource.TraceEvent(TraceEventType.Error, 4, "Error message."); mySource.TraceInformation("Informational message."); } }
Imports System.Diagnostics Class TraceTest Private Shared mySource As New TraceSource("TraceSourceApp") Shared Sub Main(ByVal args() As String) ' Issue an error and a warning message. Only the error message ' should be logged. Activity1() ' Save the original settings from the configuration file. Dim configFilter As EventTypeFilter = CType(mySource.Listeners("console").Filter, EventTypeFilter) ' Create a new event type filter that ensures ' warning messages will be written. mySource.Listeners("console").Filter = New EventTypeFilter(SourceLevels.Warning) ' Allow the trace source to send messages to listeners ' for all event types. This statement will override ' any settings in the configuration file. ' If you do not change the switch level, the event filter ' changes have no effect. mySource.Switch.Level = SourceLevels.All ' Issue a warning and a critical message. Both should be logged. Activity2() ' Restore the original filter settings. mySource.Listeners("console").Filter = configFilter Activity3() mySource.Close() Return End Sub Shared Sub Activity1() mySource.TraceEvent(TraceEventType.Error, 1, "Error message.") mySource.TraceEvent(TraceEventType.Warning, 2, "Warning message.") End Sub Shared Sub Activity2() mySource.TraceEvent(TraceEventType.Critical, 3, "Critical message.") mySource.TraceEvent(TraceEventType.Warning, 2, "Warning message.") End Sub Shared Sub Activity3() mySource.TraceEvent(TraceEventType.Error, 4, "Error message.") mySource.TraceInformation("Informational message.") End Sub End Class
將應用程式組態檔加入至專案 (如果專案中尚未存在該檔案),以初始化步驟 1 的程式碼範例中名為
TraceSourceApp
的追蹤來源。將預設的組態檔內容取代為下列設定,以初始化主控台追蹤接聽程式,以及步驟 1 中所建立追蹤來源的文字寫入器追蹤接聽程式。
<configuration> <system.diagnostics> <sources> <source name="TraceSourceApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch"> <listeners> <add name="console" type="System.Diagnostics.ConsoleTraceListener"> <filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/> </add> <add name="myListener"/> <remove name="Default"/> </listeners> </source> </sources> <switches> <add name="sourceSwitch" value="Error"/> </switches> <sharedListeners> <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener.log"> <filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/> </add> </sharedListeners> </system.diagnostics> </configuration>
除了設定追蹤接聽程式之外,組態檔也會針對這兩個接聽程式建立篩選條件,並為追蹤來源建立來源參數。 此外,有示範兩個技巧來加入追蹤接聽項:將接聽項直接加入到追蹤來源,並將接聽項加入到共用接聽項集合,然後根據名稱將它加入到追蹤來源。 會使用不同的來源層級來初始化這兩個接聽項所識別的篩選條件, 如此只會讓其中一個接聽項寫入某些訊息。
組態檔會在初始化應用程式時,初始化追蹤來源的設定。 應用程式可以動態變更組態檔所設定的屬性,以覆寫使用者指定的任何設定。 例如,您可能會想要確保重大訊息一定會傳送到文字檔,不論目前的組態設定為何。 範例程式碼將示範如何覆寫組態檔設定,以確保重大訊息會輸出到追蹤接聽程式。
在應用程式執行時變更組態檔設定,並不會變更最初的設定; 若要變更設定,您必須重新啟動應用程式,或是使用 Trace.Refresh 方法,以程式設計方式重新整理應用程式。
若要在不使用組態檔的情況下初始化追蹤來源、接聽項和篩選條件
使用下列範例程式碼可透過追蹤來源進行追蹤,而不需要使用組態檔。 這不是建議的做法,但是在您不想要根據組態檔來確保追蹤作業的情況下,可以採取這個做法。
using System; using System.Diagnostics; using System.Threading; namespace TraceSourceApp { class Program { private static TraceSource mySource = new TraceSource("TraceSourceApp"); static void Main(string[] args) { mySource.Switch = new SourceSwitch("sourceSwitch", "Error"); mySource.Listeners.Remove("Default"); TextWriterTraceListener textListener = new TextWriterTraceListener("myListener.log"); ConsoleTraceListener console = new ConsoleTraceListener(false); console.Filter = new EventTypeFilter(SourceLevels.Information); console.Name = "console"; textListener.Filter = new EventTypeFilter(SourceLevels.Error); mySource.Listeners.Add(console); mySource.Listeners.Add(textListener); Activity1(); // Allow the trace source to send messages to // listeners for all event types. Currently only // error messages or higher go to the listeners. // Messages must get past the source switch to // get to the listeners, regardless of the settings // for the listeners. mySource.Switch.Level = SourceLevels.All; // Set the filter settings for the // console trace listener. mySource.Listeners["console"].Filter = new EventTypeFilter(SourceLevels.Critical); Activity2(); // Change the filter settings for the console trace listener. mySource.Listeners["console"].Filter = new EventTypeFilter(SourceLevels.Information); Activity3(); mySource.Close(); return; } static void Activity1() { mySource.TraceEvent(TraceEventType.Error, 1, "Error message."); mySource.TraceEvent(TraceEventType.Warning, 2, "Warning message."); } static void Activity2() { mySource.TraceEvent(TraceEventType.Critical, 3, "Critical message."); mySource.TraceInformation("Informational message."); } static void Activity3() { mySource.TraceEvent(TraceEventType.Error, 4, "Error message."); mySource.TraceInformation("Informational message."); } } }
Imports System.Diagnostics Imports System.Threading Class Program Private Shared mySource As New TraceSource("TraceSourceApp") Shared Sub Main(ByVal args() As String) mySource.Switch = New SourceSwitch("sourceSwitch", "Error") mySource.Listeners.Remove("Default") Dim textListener As New TextWriterTraceListener("myListener.log") Dim console As New ConsoleTraceListener(False) console.Filter = New EventTypeFilter(SourceLevels.Information) console.Name = "console" textListener.Filter = New EventTypeFilter(SourceLevels.Error) mySource.Listeners.Add(console) mySource.Listeners.Add(textListener) Activity1() ' Allow the trace source to send messages to ' listeners for all event types. Currently only ' error messages or higher go to the listeners. ' Messages must get past the source switch to ' get to the listeners, regardless of the settings ' for the listeners. mySource.Switch.Level = SourceLevels.All ' Set the filter settings for the ' console trace listener. mySource.Listeners("console").Filter = New EventTypeFilter(SourceLevels.Critical) Activity2() ' Change the filter settings for the console trace listener. mySource.Listeners("console").Filter = New EventTypeFilter(SourceLevels.Information) Activity3() mySource.Close() Return End Sub Shared Sub Activity1() mySource.TraceEvent(TraceEventType.Error, 1, "Error message.") mySource.TraceEvent(TraceEventType.Warning, 2, "Warning message.") End Sub Shared Sub Activity2() mySource.TraceEvent(TraceEventType.Critical, 3, "Critical message.") mySource.TraceInformation("Informational message.") End Sub Shared Sub Activity3() mySource.TraceEvent(TraceEventType.Error, 4, "Error message.") mySource.TraceInformation("Informational message.") End Sub End Class