TraceSwitch.Level Propriedade
Definição
Obtém ou define o nível de rastreamento que determina as mensagens que o comutador permite.Gets or sets the trace level that determines the messages the switch allows.
public:
property System::Diagnostics::TraceLevel Level { System::Diagnostics::TraceLevel get(); void set(System::Diagnostics::TraceLevel value); };
public System.Diagnostics.TraceLevel Level { get; set; }
member this.Level : System.Diagnostics.TraceLevel with get, set
Public Property Level As TraceLevel
Valor da propriedade
Um dos TraceLevel valores que especifica o nível de mensagens permitido pela opção.One of the TraceLevel values that specifies the level of messages that are allowed by the switch.
Exceções
Level é definido para um valor que não é um dos valores TraceLevel.Level is set to a value that is not one of the TraceLevel values.
Exemplos
O exemplo de código a seguir cria um novo TraceSwitch e usa a opção para determinar se as mensagens de erro devem ser impressas.The following code example creates a new TraceSwitch and uses the switch to determine whether to print error messages. A opção é criada no nível de classe.The switch is created at the class level. MyMethod gravará a primeira mensagem de erro se a Level propriedade for definida como TraceLevel.Error ou superior.MyMethod writes the first error message if the Level property is set to TraceLevel.Error or higher. No entanto, MyMethod o não gravará a segunda mensagem de erro se Level for menor que TraceLevel.Verbose .However, MyMethod does not write the second error message if the Level is less than TraceLevel.Verbose.
// Class-level declaration.
/* Create a TraceSwitch to use in the entire application.*/
private:
static TraceSwitch^ mySwitch = gcnew TraceSwitch( "mySwitch","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("mySwitch", "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("mySwitch", "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
Comentários
Para definir o nível de seu TraceSwitch , edite o arquivo de configuração que corresponde ao nome do seu aplicativo.To set the level of your TraceSwitch, edit the configuration file that corresponds to the name of your application. Nesse arquivo, você pode adicionar uma opção e definir seu valor, remover uma opção ou limpar todas as opções definidas anteriormente pelo aplicativo.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. O arquivo de configuração deve ser formatado como o exemplo a seguir:The configuration file should be formatted like the following example:
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="0" />
<add name="myNewSwitch" value="3" />
<remove name="mySwitch" />
<clear/>
</switches>
</system.diagnostics>
</configuration>
Observação
No .NET Framework versão 2.0, você pode usar o texto para especificar o valor de uma opção.In the .NET Framework version 2.0, you can use text to specify the value for a switch. Por exemplo, true para um BooleanSwitch ou o texto que representa um valor de enumeração, como Error para um TraceSwitch .For example, true for a BooleanSwitch or the text representing an enumeration value, such as Error for a TraceSwitch. A linha <add name="mySwitch" value="Error" /> é equivalente a <add name="mySwitch" value="1" />.The line <add name="mySwitch" value="Error" /> is equivalent to <add name="mySwitch" value="1" />.
Quando o TraceSwitch Construtor não pode localizar as configurações de comutador iniciais no arquivo de configuração, a Level propriedade da nova opção é definida como TraceLevel.Off .When the TraceSwitch constructor cannot find initial switch settings in the configuration file, the Level property of the new switch is set to TraceLevel.Off.
Definir essa propriedade atualiza as TraceError TraceWarning Propriedades,, e TraceInfo TraceVerbose para refletir o novo valor.Setting this property updates the TraceError, TraceWarning, TraceInfo, and TraceVerbose properties to reflect the new value.