TraceSwitch.TraceWarning 屬性

定義

取得值,表示此參數是否允許警告訊息。

C#
public bool TraceWarning { get; }

屬性值

如果 Level 設定為 WarningInfoVerbose,則為 true,否則為 false

範例

下列程式代碼範例會建立新的 TraceSwitch ,並使用 參數來判斷是否要發出錯誤訊息。 參數會在類別層級建立。 MyMethod如果屬性設定為 TraceLevel.Warning 或更新版本,則會Level寫入第一個錯誤訊息。 不過,MyMethod當小於 TraceLevel.VerboseLevel,不會寫入第二個錯誤訊息。

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 Warning or higher.
    if (mySwitch.TraceWarning)
        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();
}

備註

您可以搭配 DebugTrace 類別使用TraceErrorTraceInfoTraceWarningTraceVerbose 屬性,以發出具有指定重要性或更高的所有訊息。 Level當屬性設定為 TraceLevel.Warning時,就會發出警告和錯誤處理訊息。

適用於

產品 版本
.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
.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

另請參閱