TraceSwitch Konstruktory
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Inicializuje novou instanci TraceSwitch třídy .
Přetížení
TraceSwitch(String, String) |
Inicializuje novou instanci TraceSwitch třídy pomocí zadaného zobrazovaného názvu a popisu. |
TraceSwitch(String, String, String) |
Inicializuje novou instanci TraceSwitch třídy pomocí zadaného zobrazovaného názvu, popisu a výchozí hodnoty přepínače. |
TraceSwitch(String, String)
- Zdroj:
- TraceSwitch.cs
- Zdroj:
- TraceSwitch.cs
- Zdroj:
- TraceSwitch.cs
Inicializuje novou instanci TraceSwitch třídy pomocí zadaného zobrazovaného názvu a popisu.
public:
TraceSwitch(System::String ^ displayName, System::String ^ description);
public TraceSwitch (string displayName, string? description);
public TraceSwitch (string displayName, string description);
new System.Diagnostics.TraceSwitch : string * string -> System.Diagnostics.TraceSwitch
Public Sub New (displayName As String, description As String)
Parametry
- displayName
- String
Název, který se má zobrazit v uživatelském rozhraní.
- description
- String
Popis přepínače.
Příklady
Následující příklad kódu vytvoří nový TraceSwitch a pomocí přepínače určí, zda se mají vytisknout chybové zprávy. Přepínač se vytvoří na úrovni třídy. MyMethod
zapíše první chybovou zprávu, pokud je vlastnost nastavena Level na TraceLevel.Error nebo vyšší. Nezapíše ale druhou chybovou zprávu, MyMethod
pokud Level je hodnota menší než TraceLevel.Verbose.
// Class-level declaration.
/* Create a TraceSwitch to use in the entire application.*/
private:
static TraceSwitch^ mySwitch = gcnew TraceSwitch( "General", "Entire Application" );
public:
static 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." );
}
static void main()
{
// Run the method that prints error messages based on the switch level.
MyMethod();
}
//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();
}
' Class-level declaration.
' Create a TraceSwitch to use in the entire application.
Private Shared mySwitch As New TraceSwitch("General", "Entire Application")
Public Shared Sub MyMethod()
' Write the message if the TraceSwitch level is set to Error or higher.
If mySwitch.TraceError Then
Console.WriteLine("My error message.")
End If
' Write the message if the TraceSwitch level is set to Verbose.
If mySwitch.TraceVerbose Then
Console.WriteLine("My second error message.")
End If
End Sub
Public Shared Sub Main()
' Run the method that prints error messages based on the switch level.
MyMethod()
End Sub
Poznámky
Pokud chcete nastavit úroveň aplikace rozhraní .NET Framework, upravte TraceSwitchkonfigurační soubor, který odpovídá názvu vaší aplikace. V tomto souboru můžete přidat přepínač a nastavit jeho hodnotu, odebrat přepínač nebo vymazat všechny přepínače, které aplikace dříve nastavila. Konfigurační soubor by měl být formátovaný jako v následujícím příkladu:
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="1" />
</switches>
</system.diagnostics>
</configuration>
K zadání hodnoty přepínače můžete použít také text. Například true
pro BooleanSwitch text nebo představující hodnotu výčtuTraceSwitch, například Error
pro . Řádek <add name="mySwitch" value="Error" />
je ekvivalentní k <add name="mySwitch" value="1" />
.
V aplikaci můžete použít nakonfigurovanou úroveň přepínače tak, že vytvoříte TraceSwitch objekt se stejným názvem, jak je znázorněno v následujícím příkladu:
private:
static TraceSwitch^ appSwitch = gcnew TraceSwitch("mySwitch",
"Switch in config file");
public:
static void Main(array<String^>^ args)
{
//...
Console::WriteLine("Trace switch {0} configured as {1}",
appSwitch->DisplayName, appSwitch->Level.ToString());
if (appSwitch->TraceError)
{
//...
}
}
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)
{
//...
}
}
Private Shared appSwitch As new TraceSwitch("mySwitch", _
"Switch in config file")
Public Shared Sub Main(args As String())
'...
Console.WriteLine("Trace switch {0} configured as {1}",
appSwitch.DisplayName, appSwitch.Level.ToString())
If appSwitch.TraceError = True Then
'...
End If
End Sub
Tento konstruktor nastaví Level vlastnost nového přepínače na TraceLevel.Off. Nebo pro aplikace .NET Framework se nastavení přepínače získá z konfiguračního souboru, pokud je k dispozici.
Třída TraceSwitch poskytuje TraceErrorvlastnosti , TraceWarning, TraceInfoa TraceVerbose pro otestování Level přepínače. Vlastnost Level získá nebo nastaví přepínač TraceLevel.
Poznámka
Pokud chcete zvýšit výkon, můžete ve třídě vytvořit TraceSwitch členy static
.
Viz také
Platí pro
TraceSwitch(String, String, String)
- Zdroj:
- TraceSwitch.cs
- Zdroj:
- TraceSwitch.cs
- Zdroj:
- TraceSwitch.cs
Inicializuje novou instanci TraceSwitch třídy pomocí zadaného zobrazovaného názvu, popisu a výchozí hodnoty přepínače.
public:
TraceSwitch(System::String ^ displayName, System::String ^ description, System::String ^ defaultSwitchValue);
public TraceSwitch (string displayName, string? description, string defaultSwitchValue);
public TraceSwitch (string displayName, string description, string defaultSwitchValue);
new System.Diagnostics.TraceSwitch : string * string * string -> System.Diagnostics.TraceSwitch
Public Sub New (displayName As String, description As String, defaultSwitchValue As String)
Parametry
- displayName
- String
Název, který se má zobrazit v uživatelském rozhraní.
- description
- String
Popis přepínače.
- defaultSwitchValue
- String
Výchozí hodnota přepínače.
Poznámky
Parametr displayName
se používá k nastavení hodnoty DisplayName vlastnosti, description
parametr slouží k nastavení hodnoty Description vlastnosti a defaultSwitchValue
parametr se uloží jako pole a použije se k inicializaci Value vlastnosti při prvním odkazu. Další informace a příklad kódu najdete v TraceSwitch(String, String) konstruktoru.