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

  • 相关阅读:
    linux & xp 双系统 重装的问题
    判断推理类试题的复言命题考点与题型总结
    Oracle、MySQL、SQL Server数据库的数据类型的差异
    java环境变量设置和问题及解决方法
    如何正确卸载MySQL,主要是删除注册表中的垃圾信息
    J2EE经典面试题及答案
    正则表达式
    囚犯的两难处境
    MySQL 数值数据类型
    linux学习之SHELL脚本
  • 原文地址:https://www.cnblogs.com/aot/p/2907774.html
Copyright © 2011-2022 走看看