[英]System.Diagnostics.Debug.Assert - how to disable in a .NET Core console app?
Consider the following simple program:考虑以下简单程序:
using System;
namespace DebugAssertTesting
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Debug.Assert(6 == 7, "Bang!");
Console.WriteLine("Hello World!");
}
}
}
When compiled as a .NET Framework console app and executed, the Assert triggers and the Assert UI dialog box is displayed.当编译为 .NET Framework 控制台应用程序并执行时,Assert 触发器和 Assert UI 对话框将显示。 (Assert failed. Abort, Retry, Ignore etc) By adding an App.Config file containing the following, the Assert UI is disabled and "Hello World." (断言失败。中止、重试、忽略等)通过添加包含以下内容的 App.Config 文件,断言 UI 被禁用并显示“Hello World”。 is displayed.被展示。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<assert assertuienabled="false" />
</system.diagnostics>
</configuration>
When the same code is compiled as a .NET Core console app and executed, the Assert triggers and the app terminates.当相同的代码被编译为 .NET Core 控制台应用程序并执行时,断言触发并且应用程序终止。 Instead of a UI dialog box, the console displays this:控制台显示以下内容,而不是 UI 对话框:
Process terminated. Assertion failed.
Bang!
at DebugAssertTesting.Program.Main(String[] args) in C:\Development\DebugAssert\DebugAssert\Program.cs:line 9
C:\Development\DebugAssert\DebugAssert\bin\Debug\netcoreapp3.1\DebugAssert.exe (process 38028) exited with code -2146232797.
Is there a way to disable the Assert within a .NET Core console app like there is in a .NET Framework app?有没有办法像 .NET 框架应用程序那样在 .NET 核心控制台应用程序中禁用断言? Adding an App.Config file with the content above does not affect the behaviour.添加包含上述内容的 App.Config 文件不会影响行为。 (VS2019 copies the file to the output folder, renaming it as expected but the app still terminates on the Assert.) (VS2019 将文件复制到 output 文件夹,按预期重命名,但应用程序仍终止于 Assert。)
Can the Assert behaviour be controlled by settings within the app's runtimeconfig.json file? Assert 行为是否可以通过应用程序的 runtimeconfig.json 文件中的设置来控制? If so, does anyone know the correct syntax?如果是这样,有人知道正确的语法吗? I've tried a few so far without any luck.到目前为止,我已经尝试了一些但没有任何运气。 I've searched for the equivalent of assertuienabled and drawn a blank.我已经搜索了 assertuienabled 的等价物并画了一个空白。 The Microsoft documentation for Debug.Assert clearly shows that it is supported in all .NET Core variants, but only mentions controlling via App.Config. Microsoft 的 Debug.Assert 文档清楚地表明它在所有 .NET Core 变体中均受支持,但仅提及通过 App.Config 进行控制。
You can remove the DEBUG
constant from DefineConstants
property in csproj
file, change this您可以从csproj
文件的DefineConstants
属性中删除DEBUG
常量,更改此
<DefineConstants>DEBUG;TRACE</DefineConstants>
to just只是
<DefineConstants>TRACE</DefineConstants>
Or uncheck the checkbox on Build
tab of project properties或者取消选中项目属性的Build
选项卡上的复选框
Another option is remove the DefaultTraceListener
from Trace.Listeners
collection另一种选择是从Trace.Listeners
集合中删除DefaultTraceListener
Trace.Listeners.RemoveAt(0);
Debug.Assert(6 == 7, "Bang!");
Console.WriteLine("Hello World!");
It'll print you Hello World!
它会打印出Hello World!
text.文本。
You can also add new listener and set AssertUiEnabled
or LogFileName
properties in code (in the similar way with app.config
in .NET Framework).您还可以添加新的侦听器并在代码中设置AssertUiEnabled
或LogFileName
属性(与 .NET Framework 中的app.config
类似)。
Last option is to override Fail
method to customize the Assert
behavior, have a look at Customizing Assert behavior article for details最后一个选项是覆盖Fail
方法来自定义Assert
行为,请查看自定义断言行为一文了解详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.