Switch Třída
Definice
Poskytuje abstraktní základní třídu pro vytvoření nových přepínačů ladění a trasování.Provides an abstract base class to create new debugging and tracing switches.
public ref class Switch abstract
public abstract class Switch
type Switch = class
Public MustInherit Class Switch
- Dědičnost
-
Switch
- Odvozené
Příklady
Následující příklad ukazuje, jak definovat novou Switch třídu se čtyřmi úrovněmi trasování, které lze použít pro trasování zásobníku volání.The following example shows how to define a new Switch class with four levels of tracing that can be used to trace a call stack. Můžete použít přepínač k instrumentaci aplikace tak, aby se protokoloval při každém zadání nebo ukončení metody.You can use the switch to instrument your application to log each time the method is entered or exited.
První příklad vytvoří výčet, který slouží k nastavení úrovně přepínače.The first example creates the enumeration used to set the level of the switch.
// The following are possible values for the new switch.
public enum class MethodTracingSwitchLevel
{
Off = 0,
EnteringMethod = 1,
ExitingMethod = 2,
Both = 3
};
// The following are possible values for the new switch.
public enum MethodTracingSwitchLevel
{
Off = 0,
EnteringMethod = 1,
ExitingMethod = 2,
Both = 3,
}
' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
Off = 0
EnteringMethod = 1
ExitingMethod = 2
Both = 3
End Enum 'MethodTracingSwitchLevel
Následující příklad vytvoří nový přepínač.The following example creates the new switch. Kód implementuje Level
vlastnost pro nastavení hodnoty nového přepínače.The code implements a Level
property to set the value of the new switch. Level
volá chráněnou vlastnost SwitchSetting , která přiřadí hodnotu novému přepínači.Level
calls the protected property SwitchSetting that assigns the value to the new switch. Tento příklad také implementuje dvě vlastnosti hodnotitele k získání přiřazené hodnoty přepínače.This example also implements two assessor properties to get the assigned value of the switch.
public ref class MyMethodTracingSwitch: public Switch
{
protected:
bool outExit;
bool outEnter;
MethodTracingSwitchLevel level;
public:
MyMethodTracingSwitch( String^ displayName, String^ description )
: Switch( displayName, description )
{}
property MethodTracingSwitchLevel Level
{
MethodTracingSwitchLevel get()
{
return level;
}
void set( MethodTracingSwitchLevel value )
{
SetSwitchSetting( (int)value );
}
}
protected:
void SetSwitchSetting( int value )
{
if ( value < 0 )
{
value = 0;
}
if ( value > 3 )
{
value = 3;
}
level = (MethodTracingSwitchLevel)value;
outEnter = false;
if ((value == (int)MethodTracingSwitchLevel::EnteringMethod) ||
(value == (int)MethodTracingSwitchLevel::Both))
{
outEnter = true;
}
outExit = false;
if ((value == (int)MethodTracingSwitchLevel::ExitingMethod) ||
(value == (int)MethodTracingSwitchLevel::Both))
{
outExit = true;
}
}
public:
property bool OutputExit
{
bool get()
{
return outExit;
}
}
property bool OutputEnter
{
bool get()
{
return outEnter;
}
}
};
public class MyMethodTracingSwitch : Switch
{
protected bool outExit;
protected bool outEnter;
protected MethodTracingSwitchLevel level;
public MyMethodTracingSwitch(string displayName, string description) :
base(displayName, description)
{
}
public MethodTracingSwitchLevel Level
{
get
{
return level;
}
set
{
SetSwitchSetting((int)value);
}
}
protected void SetSwitchSetting(int value)
{
if (value < 0)
{
value = 0;
}
if (value > 3)
{
value = 3;
}
level = (MethodTracingSwitchLevel)value;
outEnter = false;
if ((value == (int)MethodTracingSwitchLevel.EnteringMethod) ||
(value == (int)MethodTracingSwitchLevel.Both))
{
outEnter = true;
}
outExit = false;
if ((value == (int)MethodTracingSwitchLevel.ExitingMethod) ||
(value == (int)MethodTracingSwitchLevel.Both))
{
outExit = true;
}
}
public bool OutputExit
{
get
{
return outExit;
}
}
public bool OutputEnter
{
get
{
return outEnter;
}
}
}
Public Class MyMethodTracingSwitch
Inherits Switch
Protected outExit As Boolean
Protected outEnter As Boolean
Protected myLevel As MethodTracingSwitchLevel
Public Sub New(displayName As String, description As String)
MyBase.New(displayName, description)
End Sub
Public Property Level() As MethodTracingSwitchLevel
Get
Return myLevel
End Get
Set
SetSwitchSetting(CInt(value))
End Set
End Property
Protected Sub SetSwitchSetting(value As Integer)
If value < 0 Then
value = 0
End If
If value > 3 Then
value = 3
End If
myLevel = CType(value, MethodTracingSwitchLevel)
outEnter = False
If value = CInt(MethodTracingSwitchLevel.EnteringMethod) Or _
value = CInt(MethodTracingSwitchLevel.Both) Then
outEnter = True
End If
outExit = False
If value = CInt(MethodTracingSwitchLevel.ExitingMethod) Or _
value = CInt(MethodTracingSwitchLevel.Both) Then
outExit = True
End If
End Sub
Public ReadOnly Property OutputExit() As Boolean
Get
Return outExit
End Get
End Property
Public ReadOnly Property OutputEnter() As Boolean
Get
Return outEnter
End Get
End Property
End Class
Následující příklad vytvoří nový přepínač v Main
.The following example creates a new switch in Main
. Vytvoří nový přepínač a přiřadí mu hodnotu.It creates a new switch and assigns it a value. V závislosti na nastavení přepínače pak výstupy vypíše zprávy ladění pro zadání a ukončení metody.Then, depending on the switch settings, it outputs debugging messages for entering and leaving the method.
public ref class Class1
{
private:
/* Create an instance of MyMethodTracingSwitch.*/
static MyMethodTracingSwitch^ mySwitch =
gcnew MyMethodTracingSwitch( "Methods","Trace entering and exiting method" );
public:
static void main()
{
// Add the console listener to see trace messages as console output
Trace::Listeners->Add(gcnew ConsoleTraceListener(true));
Debug::AutoFlush = true;
// Set the switch level to both enter and exit
mySwitch->Level = MethodTracingSwitchLevel::Both;
// Write a diagnostic message if the switch is set to entering.
Debug::WriteLineIf(mySwitch->OutputEnter, "Entering Main");
// Insert code to handle processing here...
// Write another diagnostic message if the switch is set to exiting.
Debug::WriteLineIf(mySwitch->OutputExit, "Exiting Main");
}
};
public class Class1
{
/* Create an instance of MyMethodTracingSwitch.*/
static MyMethodTracingSwitch mySwitch =
new MyMethodTracingSwitch("Methods", "Trace entering and exiting method");
public static void Main()
{
// Add the console listener to see trace messages as console output
Trace.Listeners.Add(new ConsoleTraceListener(true));
Debug.AutoFlush = true;
// Set the switch level to both enter and exit
mySwitch.Level = MethodTracingSwitchLevel.Both;
// Write a diagnostic message if the switch is set to entering.
Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");
// Insert code to handle processing here...
// Write another diagnostic message if the switch is set to exiting.
Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main");
}
}
Public Class Class1
' Create an instance of MyMethodTracingSwitch.
Private Shared mySwitch As New _
MyMethodTracingSwitch("Methods", "Trace entering and exiting method")
Public Shared Sub Main()
' Add the console listener to see trace messages as console output
Trace.Listeners.Add(New ConsoleTraceListener(True))
Debug.AutoFlush = True
' Set the switch level to both enter and exit
mySwitch.Level = MethodTracingSwitchLevel.Both
' Write a diagnostic message if the switch is set to entering.
Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main")
' Insert code to handle processing here...
' Write another diagnostic message if the switch is set to exiting.
Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main")
End Sub
End Class
Poznámky
Přepínač poskytuje účinný mechanismus pro řízení trasování a ladění výstupu za běhu pomocí externích nastavení.A switch provides an efficient mechanism for controlling tracing and debugging output at run time using external settings. SwitchTřída implementuje výchozí chování pro přepínače, což vám umožní změnit úroveň přepínání za běhu.The Switch class implements default behavior for switches, allowing you to change the switch level at run time.
Tato třída je základní třídou třídy BooleanSwitch SourceSwitch a TraceSwitch třídy.This class is the base class for the BooleanSwitch, SourceSwitch and the TraceSwitch classes. Tyto přepínače vyhovují většině potřeb ladění a trasování.These switches meet most debugging and tracing needs. Další informace o přepínačích trasování naleznete v tématu Trace switchs.For more information about trace switches, see Trace Switches.
Chcete-li použít přepínač, je nutné povolit trasování nebo ladění.You must enable tracing or debugging to use a switch. Následující syntaxe je specifická pro kompilátor.The following syntax is compiler specific. Pokud používáte jiné kompilátory než C# nebo Visual Basic, přečtěte si dokumentaci k vašemu kompilátoru.If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.
Chcete-li povolit ladění v jazyce C#, přidejte
/d:DEBUG
příznak do příkazového řádku kompilátoru při kompilování kódu nebo můžete přidat#define DEBUG
do horní části souboru.To enable debugging in C#, add the/d:DEBUG
flag to the compiler command line when you compile your code, or you can add#define DEBUG
to the top of your file. V Visual Basic přidejte/d:DEBUG=True
příznak do příkazového řádku kompilátoru.In Visual Basic, add the/d:DEBUG=True
flag to the compiler command line.Chcete-li povolit trasování pomocí v jazyce C#, přidejte
/d:TRACE
příznak do příkazového řádku kompilátoru při kompilování kódu nebo přidejte#define TRACE
na začátek souboru.To enable tracing using in C#, add the/d:TRACE
flag to the compiler command line when you compile your code, or add#define TRACE
to the top of your file. V Visual Basic přidejte/d:TRACE=True
příznak do příkazového řádku kompilátoru.In Visual Basic, add the/d:TRACE=True
flag to the compiler command line.
Chcete-li nastavit úroveň přepínače, upravte konfigurační soubor, který odpovídá názvu vaší aplikace.To set the level of your switch, edit the configuration file that corresponds to the name of your application. V tomto souboru můžete přidat přepínač a nastavit jeho hodnotu, odebrat přepínač nebo zrušit všechny přepínače, které dříve nastavila aplikace.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. Konfigurační soubor by měl být naformátován podobně jako v následujícím příkladu:The configuration file should be formatted like the following example:
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="true" />
</switches>
</system.diagnostics>
</configuration>
Tento ukázkový konfigurační oddíl definuje BooleanSwitch DisplayName vlastnost s vlastností nastavenou na mySwitch
hodnotu a Enabled hodnotou nastavenou na true
.This example configuration section defines a BooleanSwitch with the DisplayName property set to mySwitch
and the Enabled value set to true
. V rámci aplikace můžete použít konfigurovanou hodnotu přepínače vytvořením BooleanSwitch stejného názvu, jak je znázorněno v následujícím příkladu kódu.Within your application, you can use the configured switch value by creating a BooleanSwitch with the same name, as shown in the following code example.
private:
static BooleanSwitch^ boolSwitch = gcnew BooleanSwitch("mySwitch",
"Switch in config file");
public:
static void Main( )
{
//...
Console::WriteLine("Boolean switch {0} configured as {1}",
boolSwitch->DisplayName, ((Boolean^)boolSwitch->Enabled)->ToString());
if (boolSwitch->Enabled)
{
//...
}
}
private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch",
"Switch in config file");
public static void Main( )
{
//...
Console.WriteLine("Boolean switch {0} configured as {1}",
boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
if (boolSwitch.Enabled)
{
//...
}
}
Poznámky pro implementátory
Pokud potřebujete úrovně trasování nebo mechanismy pro nastavení úrovní přepínačů, které se liší od funkcí poskytovaných BooleanSwitch SourceSwitch a TraceSwitch , můžete dědit z Switch .If you need trace levels, or mechanisms for setting switch levels different from those provided by BooleanSwitch, SourceSwitch and TraceSwitch, you can inherit from Switch. Při dědění z této třídy je nutné implementovat SwitchSetting metodu.When inheriting from this class, you must implement the SwitchSetting method.
Konstruktory
Switch(String, String) |
Inicializuje novou instanci Switch třídy.Initializes a new instance of the Switch class. |
Switch(String, String, String) |
Inicializuje novou instanci Switch třídy a určí zobrazovaný název, popis a výchozí hodnotu pro přepínač.Initializes a new instance of the Switch class, specifying the display name, description, and default value for the switch. |
Vlastnosti
Attributes |
Získá atributy vlastního přepínače definované v konfiguračním souboru aplikace.Gets the custom switch attributes defined in the application configuration file. |
Description |
Získá popis přepínače.Gets a description of the switch. |
DisplayName |
Získá název, který slouží k identifikaci přepínače.Gets a name used to identify the switch. |
SwitchSetting |
Získá nebo nastaví aktuální nastavení pro tento přepínač.Gets or sets the current setting for this switch. |
Value |
Získá nebo nastaví hodnotu přepínače.Gets or sets the value of the switch. |
Metody
Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu.Determines whether the specified object is equal to the current object. (Zděděno od Object) |
GetHashCode() |
Slouží jako výchozí funkce hash.Serves as the default hash function. (Zděděno od Object) |
GetSupportedAttributes() |
Načte vlastní atributy podporované přepínačem.Gets the custom attributes supported by the switch. |
GetType() |
Získá Type aktuální instanci.Gets the Type of the current instance. (Zděděno od Object) |
MemberwiseClone() |
Vytvoří kopii aktuálního seznamu Object .Creates a shallow copy of the current Object. (Zděděno od Object) |
OnSwitchSettingChanged() |
Vyvolá se při SwitchSetting změně vlastnosti.Invoked when the SwitchSetting property is changed. |
OnValueChanged() |
Vyvolá se při Value změně vlastnosti.Invoked when the Value property is changed. |
ToString() |
Vrátí řetězec, který představuje aktuální objekt.Returns a string that represents the current object. (Zděděno od Object) |