Click here to Skip to main content
15,883,749 members
Articles / Web Development / ASP.NET
Article

Enable Debug.Assert under ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.06/5 (9 votes)
22 Jan 2003 80.2K   1.1K   37   4
Enable Debug.Assert under ASP.NET just like under windows Forms

Sample Image - DebugListener.png

Introduction

Class System.Diagnostics.Debug provides a serial of Debug methods, such as Debug.Assert, Debug.Fail. They only work under Windows.Form applications. Under ASP.NET, no popup window will show to alert user that debug version has caught a potential logic error!

Using the code

Edit your root web.config file of your ASP.NET project:

XML
<configuration>
   <system.diagnostics>
      <assert assertuienabled="false" logfilename="c:\Debug.log" />
         <trace autoflush="true" indentsize="0">
            <listeners>
              <add type="AspNetDebugListener.DebugListener,AspNetDebugListener" />
            </listeners>
         </trace>
   </system.diagnostics>
</configuration>

How it works

TraceListener class provides the abstract base class for the listeners who monitor the trace and debug output. So derive a new class that can monitor debug output too.

C#
public class DebugListener : TraceListener 
{
    public DebugListener() : this("Forks Asp.Net DebugTracer")
    {
    }

Filter the output string

DebugListener uses Environment.StackTrace to print out the stacktrace. But it will print out many useless Debug.Assert... calling frame, I use a filter to remove it:

C#
string GetStatckTraceString()
{
    string s = Environment.StackTrace;
    StringBuilder sb = new StringBuilder(s.Length);
    using (StringReader reader = new StringReader(s))
    {
        bool debugFound = false;
        for(;;)
        {
            string line = reader.ReadLine();
            if (line == null)
                break;

            if (line.IndexOf("System.Diagnostics.Debug.Assert") != -1)
            {
                debugFound = true;
                continue;
            }

            if (debugFound)
            {
                sb.Append(line);
                sb.Append(Environment.NewLine);
            }
        }
    }

    return sb.ToString();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
amittrivedimca7-Jul-09 21:31
amittrivedimca7-Jul-09 21:31 
Generalhistory of Debug.Assert Pin
Michael Freidgeim23-Nov-11 10:13
Michael Freidgeim23-Nov-11 10:13 
Generalbreak into code when calling Debug.Assert Pin
Michael Freidgeim12-Aug-04 14:23
Michael Freidgeim12-Aug-04 14:23 
GeneralOther syntax Pin
Uwe Keim26-Jan-03 19:13
sitebuilderUwe Keim26-Jan-03 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.