Debug.Assert seems not to work in Mono

2019-04-21 15:07发布

Consider the following C# program:

using System;
using System.Diagnostics;

namespace Test
{
        class MainClass
    {
        public static void Main (string[] args)
        {
            Debug.Assert(false);
            Debug.Fail("fail!");
            Console.WriteLine ("Hello World!");
        }
    }
}

When compiling this using:

dmcs -debug -d:DEBUG Main.cs

and then running it with:

mono --debug Main.exe

the assertion and fail seem to be ignored. The output is just:

Hello World!

I checked other related questions on StackOverflow, but I could not find a solution. In particular the solution give in Mono - Debug.Assert does not work does not work. (UPDATE: the updated solution does work, see below comments.)

I use Mono 2.10.5-1 on Ubuntu 11.10.

标签: c# mono
2条回答
趁早两清
2楼-- · 2019-04-21 15:44

You can use the xml configuration, or you can place it under the control of your program by adding a trace listener at runtime:

var tl = new System.Diagnostics.ConsoleTraceListener();
System.Diagnostics.Debug.Listeners.Add ( tl );

This has the added advantage of you being able to enable it after the program has started.

查看更多
再贱就再见
3楼-- · 2019-04-21 15:58

C# on mono - http://ebsteblog.wordpress.com/2009/05/06/debugassert-and-mono/

Excerpt from the article:

...if you create a .config file for your app and set the assertuienabled attribute to true, you get the same dialog as with .NET... File app.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.diagnostics>
        <assert assertuienabled="true" />
    </system.diagnostics>
</configuration>

Old answer: C++ comment if you did not specify -define DEBUG on command line/compile options.

For debug add

#define DEBUG

at the beginning of the code or

#define TRACE

for trace.

See the solution here: http://lists.ximian.com/pipermail/mono-list/2006-December/033774.html

p.s: I tried this with C++ not C#. This may not work for C#.

查看更多
登录 后发表回答