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

  • 相关阅读:
    微软职位内部推荐-Senior Software Engineer-DUT
    微软职位内部推荐-Senior PM
    面试题:打印蛇形二维数组
    微软职位内部推荐-Principal Software Eng Mgr
    微软职位内部推荐-Senior SDE
    微软职位内部推荐-Senior Software Engineer II-Search
    微软职位内部推荐-Software Engineer II-Search
    分布式锁的实现方式
    Java集合框架实现自定义排序
    Redis的缓存策略和主键失效机制
  • 原文地址:https://www.cnblogs.com/aot/p/2907774.html
Copyright © 2011-2022 走看看