Click here to Skip to main content
6,634,665 members and growing! (15,298 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Enable Debug.Assert under ASP.NET

By Red Forks

Enable Debug.Assert under ASP.NET just like under windows Forms
C#.NET 1.0, Win2K, WinXP, ASP.NET, Dev
Posted:2 Jan 2003
Updated:22 Jan 2003
Views:53,747
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.16 Rating: 3.50 out of 5
2 votes, 25.0%
1
1 vote, 12.5%
2

3
1 vote, 12.5%
4
4 votes, 50.0%
5

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:

<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.

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:

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

About the Author

Red Forks


Member

Location: China China

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralMy vote of 2 Pinmemberamittrivedimca22:31 7 Jul '09  
Generalbreak into code when calling Debug.Assert PinmemberMichael Freidgeim15:23 12 Aug '04  
GeneralOther syntax PinsitebuilderUwe Keim20:13 26 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Jan 2003
Editor: Smitha Vijayan
Copyright 2003 by Red Forks
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project