简体   繁体   English

System.Diagnostics.Debug.Assert - 如何在 .NET 核心控制台应用程序中禁用?

[英]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).您还可以添加新的侦听器并在代码中设置AssertUiEnabledLogFileName属性(与 .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.

相关问题 在Release版本中获取System.Diagnostics.Debug.Assert()的行为? - Get the behavior of System.Diagnostics.Debug.Assert() in a Release build? System.Diagnostics.Debug.Assert(true)有什么作用? - What does System.Diagnostics.Debug.Assert(true) do? C# .Net Core Console App Debug.Assert 不显示消息框 - C# .Net Core Console App Debug.Assert not showing message box .NET Core - 在 App.config 中使用 system.diagnostics - .NET Core - Using system.diagnostics in App.config 使用 dotnet run 时如何调试 .NET 核心控制台应用程序 - How to Debug a .NET Core Console App When Using dotnet run System.Diagnostics.Trace,System.Diagnostics.Debug和System.Console有什么区别? - What's the difference between System.Diagnostics.Trace, System.Diagnostics.Debug and System.Console? 如何查看/调试System.Diagnostics计数器? - How to see/debug System.Diagnostics Counter? 不使用 shell 执行时,从 System.Diagnostics.Process 运行带有 EFCore 的 .net 核心应用程序失败 - Running a .net core app with EFCore from System.Diagnostics.Process fails when not using shell execute .Net Core Console应用程序 - 调试失败 - 终止与远程端点的连接 - .Net Core Console App - Debug Fails - The connection with the remote endpoint was terminated 从Visual Studio 2017调试.Net Core Console App - Debug .Net Core Console App out of Visual Studio 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM