如何:创建和初始化跟踪源
注意
本文特定于 .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