Trace.Assert Method

Definition

Checks for a condition; if the condition is false, outputs messages and displays a message box that shows the call stack.

Overloads

Assert(Boolean)

Checks for a condition; if the condition is false, displays a message box that shows the call stack.

Assert(Boolean, String)

Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.

Assert(Boolean, String, String)

Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

Assert(Boolean)

Source:
Trace.cs
Source:
Trace.cs
Source:
Trace.cs

Checks for a condition; if the condition is false, displays a message box that shows the call stack.

C#
[System.Diagnostics.Conditional("TRACE")]
[System.Runtime.CompilerServices.OverloadResolutionPriority(-1)]
public static void Assert(bool condition);
C#
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition);

Parameters

condition
Boolean

The conditional expression to evaluate. If the condition is true, a failure message is not sent and the message box is not displayed.

Attributes

Examples

The following example creates an index for an array. Then some action is performed that sets the value of the index. Next the code calls Assert to verify the index value is valid. If it is not valid, the Assert outputs the call stack.

C#
// Create an index for an array.
int index;

void Method()
{
    // Perform some action that sets the index.

    // Test that the index value is valid.
    Trace.Assert(index > -1);
}

Remarks

Use the Trace.Assert method if you want to do assertions in release builds. The Debug.Assert method works only in debug builds. For more information, see Assertions in Managed Code.

Typically, the Assert(Boolean) method is used to identify logic errors during program development. Assert(Boolean) evaluates the condition. If the result is false, it sends a failure message to the Listeners collection. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

When the application runs in user-interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.

Note

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, you can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. The configuration file should be formatted as follows:

XML
<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="4"/>  
    </switches>  
    <trace autoflush="false" indentsize="4"/>  
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>  
  </system.diagnostics>  
</configuration>  

See also

Applies to

.NET 10 and other versions
Product Versions
.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

Assert(Boolean, String)

Source:
Trace.cs
Source:
Trace.cs
Source:
Trace.cs

Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.

C#
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string? message = default);
C#
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string? message);
C#
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string message);

Parameters

condition
Boolean

The conditional expression to evaluate. If the condition is true, the specified message is not sent and the message box is not displayed.

message
String

The message to send to the Listeners collection.

Attributes

Examples

The following example checks to see that the type parameter is valid. If the type passed in is null, the Assert outputs a message.

C#
public static void MyMethod(Type type, Type baseType)
{
    Trace.Assert(type != null, "Type parameter is null");

    // Perform some processing.
}

Remarks

Use the Trace.Assert method if you want to do assertions in release builds. The Debug.Assert method works only in debug builds. For more information, see Assertions in Managed Code.

Typically, the Assert(Boolean, String) method is used to identify logic errors during program development. Assert(Boolean, String) evaluates the condition. If the result is false, it sends the specified diagnostic message to the Listeners collection. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

When the application runs in user-interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.

Note

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, you can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. The configuration file should be formatted as follows:

XML
<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="4"/>  
    </switches>  
    <trace autoflush="false" indentsize="4"/>  
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>  
  </system.diagnostics>  
</configuration>  

See also

Applies to

.NET 10 and other versions
Product Versions
.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

Assert(Boolean, String, String)

Source:
Trace.cs
Source:
Trace.cs
Source:
Trace.cs

Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

C#
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string? message, string? detailMessage);
C#
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string message, string detailMessage);

Parameters

condition
Boolean

The conditional expression to evaluate. If the condition is true, the specified messages are not sent and the message box is not displayed.

message
String

The message to send to the Listeners collection.

detailMessage
String

The detailed message to send to the Listeners collection.

Attributes

Examples

The following example checks to see that the type parameter is valid. If the type passed in is null, the Assert outputs a message.

C#
public static void MyMethod(Type type, Type baseType)
{
    Trace.Assert(type != null, "Type parameter is null",
       "Can't get object for null type");

    // Perform some processing.
}

Remarks

Use the Trace.Assert method if you want to do assertions in release builds. The Debug.Assert method works only in debug builds. For more information, see Assertions in Managed Code.

Typically, the Assert(Boolean, String, String) method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends the specified diagnostic message and detailed message to the Listeners collection. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

When the application runs in user-interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.

Note

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, you can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. The configuration file should be formatted as follows:

XML
<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="4"/>  
    </switches>  
    <trace autoflush="false" indentsize="4"/>  
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>  
  </system.diagnostics>  
</configuration>  

See also

Applies to

.NET 10 and other versions
Product Versions
.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