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

  • 相关阅读:
    测试文件报告
    Bug Variations
    阶段一 问答题2
    阶段一 问答题1
    HeapSort
    Git系列 (01):git clone 速度太慢解决方法
    ES6系列 (03):链判断运算符和Null 判断运算符
    ES6系列 (02):解构赋值
    ES6系列 (01):箭头函数this指向问题
    我忘却了所有,抛却了信仰,舍弃了轮回,只为,那曾在佛前哭泣的玫瑰,早已失去旧日的光泽。
  • 原文地址:https://www.cnblogs.com/aot/p/2907774.html
Copyright © 2011-2022 走看看