Läs på engelska Redigera

Dela via


TraceSwitch Constructors

Definition

Initializes a new instance of the TraceSwitch class.

Overloads

TraceSwitch(String, String)

Initializes a new instance of the TraceSwitch class, using the specified display name and description.

TraceSwitch(String, String, String)

Initializes a new instance of the TraceSwitch class, using the specified display name, description, and default value for the switch.

TraceSwitch(String, String)

Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs

Initializes a new instance of the TraceSwitch class, using the specified display name and description.

C#
public TraceSwitch(string displayName, string? description);
C#
public TraceSwitch(string displayName, string description);

Parameters

displayName
String

The name to display on a user interface.

description
String

The description of the switch.

Examples

The following code example creates a new TraceSwitch and uses the switch to determine whether to print error messages. The switch is created at the class level. MyMethod writes the first error message if the Level property is set to TraceLevel.Error or higher. However, MyMethod does not write the second error message if the Level is less than TraceLevel.Verbose.

C#
//Class-level declaration.
/* Create a TraceSwitch to use in the entire application.*/
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application");

static public void MyMethod()
{
    // Write the message if the TraceSwitch level is set to Error or higher.
    if (mySwitch.TraceError)
        Console.WriteLine("My error message.");

    // Write the message if the TraceSwitch level is set to Verbose.
    if (mySwitch.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();
}

Remarks

For .NET Framework apps, to set the level of your TraceSwitch, edit the configuration file that corresponds to the name of your application. In 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:

XML
<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="1" />  
    </switches>  
  </system.diagnostics>  
</configuration>  

You can also use text to specify the value for a switch. For example, true for a BooleanSwitch or the text representing an enumeration value, such as Error for a TraceSwitch. The line <add name="mySwitch" value="Error" /> is equivalent to <add name="mySwitch" value="1" />.

In your application, you can use the configured switch level by creating a TraceSwitch with the same name, as shown in the following example:

C#
private static TraceSwitch appSwitch = new TraceSwitch("mySwitch",
    "Switch in config file");

public static void Main(string[] args)
{
    //...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString());
    if (appSwitch.TraceError)
    {
        //...
    }
}

This constructor sets the Level property of the new switch to TraceLevel.Off. Or, for .NET Framework apps, the switch settings are obtained from the configuration file, if available.

The TraceSwitch class provides the TraceError, TraceWarning, TraceInfo, and TraceVerbose properties to test the Level of the switch. The Level property gets or sets the switch's TraceLevel.

Anteckning

To improve performance, you can make TraceSwitch members static in your class.

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TraceSwitch(String, String, String)

Source:
TraceSwitch.cs
Source:
TraceSwitch.cs
Source:
TraceSwitch.cs

Initializes a new instance of the TraceSwitch class, using the specified display name, description, and default value for the switch.

C#
public TraceSwitch(string displayName, string? description, string defaultSwitchValue);
C#
public TraceSwitch(string displayName, string description, string defaultSwitchValue);

Parameters

displayName
String

The name to display on a user interface.

description
String

The description of the switch.

defaultSwitchValue
String

The default value of the switch.

Remarks

The displayName parameter is used to set the value of the DisplayName property, the description parameter is use to set the value of the Description property, and the defaultSwitchValue parameter is saved as a field and used to initialize the Value property on first reference. See the TraceSwitch(String, String) constructor for more information and a code example.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1