zoukankan      html  css  js  c++  java
  • How to prevent Debug.Assert(…) to show a modal dialog

    Question:

    When running at Debug mode, the Debug.Assert(…) method should pop up a modal dialog by default, and this could stop current running.

    Solution:

    1. All set to Release mode for source code, and re-build again, use released assembly, this would skip Debug code snippet.

    2. If you cannot change the release mode from VS, or even you cannot get any source code, you can solve this problem by modifying the configuration file. (app.config)

    <configuration>
      <system.diagnostics>
        <trace>
          <listeners>
            <clear/>
          </listeners>
        </trace>    
        <assert assertuienabled="false" />
      </system.diagnostics>
    </configuration>

    Ref: http://msdn.microsoft.com/en-us/library/ty5e4c4h.aspx

    assertuienabled Attribute

    Value

    Description

    true

    Displays the message box. This is the default.

    false

    Does not display the message box.

    3. Or we can create our customized Trace Listener to replace the default trace listener.

    Here is Fail method in DefaultTraceListener class (System.Diagnostics.DefaultTraceListener)

    public override void Fail(string message, string detailMessage)
    {
        string str;
        StackTrace trace = new StackTrace(true);
        int startFrameIndex = 0;
        bool uiPermission = UiPermission;
        try
        {
            str = this.StackTraceToString(trace, startFrameIndex, trace.FrameCount - 1);
        }
        catch
        {
            str = "";
        }
        this.WriteAssert(str, message, detailMessage);
        if (this.AssertUiEnabled && uiPermission)
        {
            AssertWrapper.ShowAssert(str, trace.GetFrame(startFrameIndex), message, detailMessage);
        }
    }
    public static void ShowAssert(string stackTrace, StackFrame frame, string message, string detailMessage)
    {
        ShowMessageBoxAssert(stackTrace, message, detailMessage);
    }

    And it pops up messagebox dialog by invoking the ShowAssert method.

    We can now create a class that inherits from TraceListener, just need to override the Fail method. Here we output the trace message to console, no dialog pop-up.

    public class MyTraceListener : TraceListener
     {
            public override void Write(string message)
            {
                Console.Write(message);
            }
    
            public override void WriteLine(string message)
            {
                Console.WriteLine(message);
            }
    
            public override void Fail(string message)
            {
                this.WriteLine(message);
            }
    
            public override void Fail(string message, string detailMessage)
            {
                this.WriteLine(string.Format("{0}: {1}", message, detailMessage));
            }
     }

    Finally, we have to register this trace type by modifying our app configuration file.

    <configuration>
      <system.diagnostics>
        <trace>
          <listeners>
            <clear/>
            <add type="DebugAssert.MyTraceListener, DebugAssert" name="MyTraceListener" />
          </listeners>
        </trace>    
        <assert assertuienabled="false" />
      </system.diagnostics>
    </configuration>

    Reference Articles:

    http://www.alc.amadeus.com/content/public/alw/skillsoft/cbtlib/79986/80018/eng/thin/transcript.html

    How to integrate Debug.Assert with your ASP.NET web application

  • 相关阅读:
    2017年陕西省网络空间安全技术大赛WP
    XDCTF2014 Writeup
    TensorFlow入门测试程序
    python计算器
    CentOS安装crontab及使用方法
    在Linux 双机下自己手动实现浮动ip技术
    CentOS 7下安装配置FTP
    encodeURI 解码 编码
    jquery 消息提醒插件 toastmessage
    搭通自己的电脑与GitHub的传输通道
  • 原文地址:https://www.cnblogs.com/aot/p/2907774.html
Copyright © 2011-2022 走看看