public class TraceSwitch : Switch
|
To set the level of your TraceSwitch, edit the configuration file that corresponds to the name of your application. Within this file, you can add a switch and set its value, remove a switch, or clear all the switches previously set by the application. The configuration file should be formatted like the following example:
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="10" /> <add name="myNewSwitch" value="20" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
When the TraceSwitch.#ctor constructor cannot find initial switch settings in the configuration file, the TraceSwitch.Level of the new switch is set to TraceLevel.Off.
The TraceSwitch class provides the TraceSwitch.TraceError, TraceSwitch.TraceWarning, TraceSwitch.TraceInfo, and TraceSwitch.TraceVerbose properties test the TraceSwitch.Level of the switch. The TraceSwitch.Level property gets or sets the switch's TraceLevel.
You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.
For more information on instrumenting your application, see Debug and Trace.
MyMethod
will write the first error message if the TraceSwitch.Level is set to TraceSwitch.TraceError or higher. However,
MyMethod
will not write the second error message when the TraceSwitch.Level is less than TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(generalSwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
ctor #1 | Initializes a new instance of the TraceSwitch class. |
Description (inherited from System.Diagnostics.Switch) |
Read-only See base class member description: System.Diagnostics.Switch.Description Gets a description of the switch. |
DisplayName (inherited from System.Diagnostics.Switch) |
Read-only See base class member description: System.Diagnostics.Switch.DisplayName Gets a name used to identify the switch. |
Level | Read-write Gets or sets the trace level that specifies the messages to output for tracing and debugging. |
TraceError | Read-only Gets a value indicating whether the TraceSwitch.Level is set to Error, Warning, Info, or Verbose. |
TraceInfo | Read-only Gets a value indicating whether the TraceSwitch.Level is set to Info or Verbose. |
TraceVerbose | Read-only Gets a value indicating whether the TraceSwitch.Level is set to Verbose. |
TraceWarning | Read-only Gets a value indicating whether the TraceSwitch.Level is set to Warning, Info, or Verbose. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
SwitchSetting (inherited from System.Diagnostics.Switch) |
Read-write See base class member description: System.Diagnostics.Switch.SwitchSetting Gets or sets the current setting for this switch. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
OnSwitchSettingChanged | Overridden: Update the level for this switch. |
Hierarchy:
displayName
description
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="10" /> <add name="myNewSwitch" value="20" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
When the TraceSwitch.#ctor constructor cannot find initial switch settings in the configuration file, the TraceSwitch.Level of the new switch is set to TraceLevel.Off.
The TraceSwitch class provides the TraceSwitch.TraceError, TraceSwitch.TraceWarning, TraceSwitch.TraceInfo, and TraceSwitch.TraceVerbose properties test the TraceSwitch.Level of the switch. The TraceSwitch.Level property gets or sets the switch's TraceLevel.
MyMethod
will write the first error message if the TraceSwitch.Level is set to TraceSwitch.TraceError or higher. However,
MyMethod
will not write the second error message when the TraceSwitch.Level is less than TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(generalSwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
public string Description {get;}
|
public string DisplayName {get;}
|
public TraceLevel Level {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | Setting the level to a value not in the TraceLevel enumeration. |
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="10" /> <add name="myNewSwitch" value="20" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
When the TraceSwitch.#ctor constructor cannot find initial switch settings in the configuration file, the TraceSwitch.Level of the new switch is set to TraceLevel.Off.
Setting this property will update the TraceSwitch.TraceError, TraceSwitch.TraceWarning, TraceSwitch.TraceInfo, and TraceSwitch.TraceVerbose properties in this class to the new value.
MyMethod
will write the first error message if the TraceSwitch.Level is set to TraceSwitch.TraceError or higher. However,
MyMethod
will not write the second error message when the TraceSwitch.Level is less than TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(generalSwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
protected int SwitchSetting {get; set;}
|
public bool TraceError {get;}
|
MyMethod
will write the first error message if the TraceSwitch.Level is set to TraceSwitch.TraceError or higher. However,
MyMethod
will not write the second error message when the TraceSwitch.Level is less than TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(generalSwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
public bool TraceInfo {get;}
|
MyMethod
will write the first error message if the TraceSwitch.Level is set to TraceSwitch.TraceInfo or higher. However,
MyMethod
will not write the second error message when the TraceSwitch.Level is less than TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Info or higher. if(generalSwitch.TraceInfo) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
public bool TraceVerbose {get;}
|
MyMethod
will write both error messages when the TraceSwitch.Level is set to TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. if(generalSwitch.TraceError) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
public bool TraceWarning {get;}
|
MyMethod
will write the first error message if the TraceSwitch.Level is set to TraceSwitch.TraceWarning or higher. However,
MyMethod
will not write the second error message when the TraceSwitch.Level is less than TraceSwitch.TraceVerbose.//Class-level declaration. /* Create a TraceSwitch to use in the entire application. This switch is set * by using values stored in the registry or in environmental variables. */ static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); static public void MyMethod() { // Write the message if the TraceSwitch level is set to Warning or higher. if(generalSwitch.TraceWarning) Console.WriteLine("My error message."); // Write the message if the TraceSwitch level is set to Verbose. if(generalSwitch.TraceVerbose) Console.WriteLine("My second error message."); } public static void Main(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); }
~TraceSwitch(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
protected override void OnSwitchSettingChanged(); |
public virtual string ToString(); |