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

  • 相关阅读:
    C# a标签请求下载文件
    jquery datatable无数据提示不居中显示
    visual studio使用dos命令在生成项目时复制文件到指定目录
    技嘉,u盘安装win7,提示“找不到驱动器设备驱动程序”
    MQTT + apache-apollo服务器初学使用
    svn的基本使用方法
    linux之用户密码破解的操作
    mysql的部署
    nginx服务器
    部署
  • 原文地址:https://www.cnblogs.com/aot/p/2907774.html
Copyright © 2011-2022 走看看