简体   繁体   English

C# .Net Core Console App Debug.Assert 不显示消息框

[英]C# .Net Core Console App Debug.Assert not showing message box

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netframework-4.8

Based on this documentation the code below should generate a message box, but all it does is print to the output tab:根据本文档,下面的代码应该生成一个消息框,但它所做的只是打印到输出选项卡:

How can I get it to produce the message box as advertised in the Microsoft docs?我怎样才能让它产生 Microsoft 文档中宣传的消息框?

//*********************************************************

//****Assignment 6 Section 1

//*********************************************************

Console.WriteLine("\n Assignment 6 - Asserts and Try/Catch.");

string fooString = null;
int fooInt = 0;

Debug.Assert(!String.IsNullOrEmpty(fooString), "Parameter must not be empty or null.");
Debug.Assert(fooInt > 0, "Parameter must be greater than zero.");

Here is the code from Form1.cs (tried with and without the Trace.Listeners code)这是来自 Form1.cs 的代码(尝试使用和不使用 Trace.Listeners 代码)

namespace Unit_6_WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Trace.Listeners.Clear();
            DefaultTraceListener defaultTraceListener = new DefaultTraceListener();
            defaultTraceListener.AssertUiEnabled = true;
            Trace.Listeners.Add(defaultTraceListener);

            //*********************************************************

            //****Assignment 6 Section 1

            //*********************************************************

            Console.WriteLine("Assignment 6 - Asserts and Try/Catch. \n");

            string fooString = null;
            int fooInt = 0;


            Debug.Assert(!String.IsNullOrEmpty(fooString), "Parameter must not be empty or null.");
            Debug.Assert(fooInt > 0, "Parameter must be greater than zero.");

            //*********************************************************

            //****Assignment 6 Section 2

            //*********************************************************

            string[] fooStringArray = new string[5];

            try
            {
                for (int index = 0; index <= fooStringArray.Length; index++)
                {
                    var foo = fooStringArray[index];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Array out of bounds error occurred.");
                Console.WriteLine("{0} \n", ex.Message);
            }

            //*********************************************************

            //****Assignment 6 Section 3

            //*********************************************************

            try
            {
                using (FileStream fs = File.Open("NoFileNamedThis.txt", FileMode.Open))
                {

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("File not found error.");
                Console.WriteLine("{0} \n", ex.Message);
            }

            //*********************************************************

            //****Assignment 6 Section 4

            //*********************************************************

            try
            {
                DivideByZero(1, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("DivideByZero error occurred.");
                Console.WriteLine("{0}", ex.Message);
            }


            void DivideByZero(int dividend, int divisor)
            {
                if (divisor == 0)
                {
                    throw new ArgumentException("Divide by Zero");
                }
            }
        }

    }
}

As per Microsoft docs here:根据此处的 Microsoft 文档:

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netframework-4.8

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.消息框包含三个按钮:Abort、Retry 和 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.单击忽略继续执行代码中的下一条指令。

Since you are not running in UI mode, message is being shown on Console.由于您不是在 UI 模式下运行,因此控制台上会显示消息。

Again as per docs, to enable UI mode:再次根据文档,启用 UI 模式:

<configuration>  
  <system.diagnostics>  
    <assert assertuienabled="true"    logfilename="c:\\myFile.log" />  
   </system.diagnostics>  
 </configuration>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM