跟踪开关Trace Switches

跟踪开关用于启用、禁用和筛选跟踪输出。Trace switches enable you to enable, disable, and filter tracing output. 它们是代码中存在并可通过 .config 文件在外部配置的对象。They are objects that exist in your code and can be configured externally through the .config file. .NET Framework 中提供三种跟踪开关类型: BooleanSwitch 类、 TraceSwitch 类和 SourceSwitch 类。There are three types of trace switches provided in the .NET Framework: the BooleanSwitch class, the TraceSwitch class, and the SourceSwitch class. BooleanSwitch 类充当切换开关,可启用或禁用各种跟踪语句。The BooleanSwitch class acts as a toggle switch, either enabling or disabling a variety of trace statements. 使用 TraceSwitchSourceSwitch 类,可以启用特定跟踪级别的跟踪开关,以确保出现为该级别以及其下所有级别指定的 TraceTraceSource 消息。The TraceSwitch and SourceSwitch classes allow you to enable a trace switch for a particular tracing level so that the Trace or TraceSource messages specified for that level and all levels below it appear. 如果禁用此开关,则不会出现跟踪消息。If you disable the switch, the trace messages will not appear. 所有这些类均派生自抽象 (MustInheritT:System.Diagnostics.Switch) 类 Switch,用户开发的任何开关也应如此。All these classes derive from the abstract (MustInherit) class Switch, as should any user-developed switches.

跟踪开关可用于筛选信息。Trace switches can be useful for filtering information. 例如,你可能希望查看数据访问模块中的每条跟踪消息,但只查看应用程序其余部分的错误消息。For example, you might want to see every tracing message in a data access module, but only error messages in the rest of the application. 在这种情况下,则需要分别对数据访问模块和应用程序的其余部分使用一个跟踪开关。In that case, you would use one trace switch for the data access module and one switch for the rest of the application. 通过使用 .config 文件将开关配置为相应的设置,便可以控制接收的跟踪消息类型。By using the .config file to configure the switches to the appropriate settings, you could control what types of trace message you received. 有关详细信息,请参阅如何:创建、 初始化和配置跟踪开关For more information, see How to: Create, Initialize and Configure Trace Switches.

通常情况下,会在禁用开关的情况下执行已部署的应用程序,以确保在应用程序运行时用户不会看到屏幕上出现的或填满日志文件的不相关跟踪消息。Typically, a deployed application is executed with its switches disabled, so that users need not observe a lot of irrelevant trace messages appearing on a screen or filling up a log file as the application runs. 如果应用程序执行期间出现了问题,可以停止应用程序,启用开关,然后重新启动应用程序。If a problem arises during application execution, you can stop the application, enable the switches, and restart the application. 然后,将显示跟踪消息。Then the tracing messages will be displayed.

若要使用开关,首先必须在 BooleanSwitch T:System.Diagnostics.Switch 类、 TraceSwitch 类或开发人员定义的开关类中创建一个开关对象。To use a switch you must first create a switch object from a BooleanSwitch class, a TraceSwitch class, or a developer-defined switch class. 有关创建开发人员定义的开关的详细信息,请参阅 .NET Framework 引用中的 Switch 类。For more information about creating developer-defined switches, see the Switch class in the .NET Framework reference. 然后,将设置一个指定何时使用该开关对象的配置值。Then you set a configuration value that specifies when the switch object is to be used. 然后,可以在各种 Trace T:System.Diagnostics.Switch (或 Debug)跟踪方法中测试该开关对象的设置。You then test the setting of the switch object in various Trace (or Debug) tracing methods.

跟踪级别Trace Levels

使用 TraceSwitch T:System.Diagnostics.Switch时,存在其他注意事项。When you use TraceSwitch, there are additional considerations. TraceSwitch T:System.Diagnostics.Switch 对象具有四个属性,这四个属性返回指示开关是否设置为至少一个特殊级别的 Boolean 值:A TraceSwitch object has four properties that return Boolean values indicating whether the switch is set to at least a particular level:

通过设置级别,可以将接收的跟踪信息数量限制为仅解决问题所需的信息。Levels allow you to limit the amount of tracing information you receive to only that information needed to solve a problem. 通过将跟踪开关设置和配置为相应的跟踪级别,可以指定跟踪输出的详细级别。You specify the level of detail you want in your tracing output by setting and configuring trace switches to the appropriate trace level. 可以选择接收错误消息、警告消息、信息性消息、详细跟踪消息,也可以选择不接收任何消息。You can receive error messages, warning messages, informational messages, verbose tracing messages, or no message at all.

这完全取决于你自身确定与每个级别关联的消息类型。It is entirely up to you to decide what kind of message to associate with each level. 通常,跟踪消息的内容取决于每个级别关联的内容,但你可以确定级别之间的差异。Typically, the content of tracing messages depends on what you associate with each level, but you determine the differences between levels. 例如,你可能希望在第 3 级别 (InfoT:System.Diagnostics.Switch) 提供问题的详细说明,但在第 1 级别 (Error) 仅提供错误参考号。You might want to provide detailed descriptions of a problem at level 3 (Info), for example, but provide only an error reference number at level 1 (Error). 这完全取决于你自身确定的最适合应用程序的方案。It is entirely up to you to decide what scheme works best in your application.

这些属性与 TraceLevel T:System.Diagnostics.Switch 枚举的值 1-4 相对应。These properties correspond to the values 1 through 4 of the TraceLevel enumeration. 下表列出 TraceLevel T:System.Diagnostics.Switch 枚举的级别与对应的值。The following table lists the levels of the TraceLevel enumeration and their values.

枚举值Enumerated value 整数值Integer value 所显示的(或写入指定输出目标)的消息类型Type of message displayed (or written to a specified output target)
OffOff 00 NoneNone
ErrorError 11 仅错误消息Only error messages
警告Warning 22 警告消息和错误消息Warning messages and error messages
T:System.Diagnostics.SwitchInfo 33 信息性消息、警告消息和错误消息Informational messages, warning messages, and error messages
详细Verbose 44 详细消息、信息性消息、警告消息和错误消息Verbose messages, informational messages, warning messages, and error messages

TraceSwitch T:System.Diagnostics.Switch 属性指示开关的最大跟踪级别。The TraceSwitch properties indicate the maximum trace level for the switch. 也就是说,将在指定的级别以及其下的所有级别写入跟踪信息。That is, tracing information is written for the level specified as well as for all lower levels. 例如,如果 TraceInfo T:System.Diagnostics.Switchtrue,则 TraceErrorTraceWarning 也为 true ,但 TraceVerbose 可能为 falseFor example, if TraceInfo is true, then TraceError and TraceWarning are also true but TraceVerbose might well be false.

这些属性是只读的。These properties are read-only. 当设置了 TraceLevel 属性时,TraceSwitch T:System.Diagnostics.Switch 对象会自动对这些属性进行设置。The TraceSwitch object automatically sets them when the TraceLevel property is set. 例如:For example:

Dim myTraceSwitch As New TraceSwitch("SwitchOne", "The first switch")  
myTraceSwitch.Level = TraceLevel.Info  
' This message box displays true, because setting the level to  
' TraceLevel.Info sets all lower levels to true as well.  
MessageBox.Show(myTraceSwitch.TraceWarning.ToString())  
' This messagebox displays false.  
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString())  
System.Diagnostics.TraceSwitch myTraceSwitch =   
   new System.Diagnostics.TraceSwitch("SwitchOne", "The first switch");  
myTraceSwitch.Level = System.Diagnostics.TraceLevel.Info;  
// This message box displays true, because setting the level to   
// TraceLevel.Info sets all lower levels to true as well.  
MessageBox.Show(myTraceSwitch.TraceWarning.ToString());  
// This message box displays false.  
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString());  

开发人员定义的开关Developer-Defined Switches

除了提供 BooleanSwitch T:System.Diagnostics.SwitchTraceSwitch之外,可以通过从 Switch 类继承以及将基类方法重写为自定义的方法定义自己的开关。In addition to providing BooleanSwitch and TraceSwitch, you can define your own switches by inheriting from the Switch class and overriding the base class methods with customized methods. 有关创建开发人员定义的开关的详细信息,请参阅 .NET Framework 引用中的 Switch 类。For more information about creating developer-defined switches, see the Switch class in the .NET Framework reference.

请参阅See also