简体   繁体   中英

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

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?

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

//****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)

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:

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. 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.

Again as per docs, to enable UI mode:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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