Figures
© 2004 Microsoft Corporation. All rights reserved.
Figure 2 ASPTraceListener Properties

Property
Type
Description
ShowDebugLog
Boolean
If true, output any text to any attached debugger so you can see it in the Output window. True by default
ShowOutputDebugString
Boolean
If true, output any text through a standard OutputDebugString call. False by default
EventSource
String
The name of the event source for writing output to the Event Log. Setting this to null will stop writing. Null by default
Writer
TextWriter
If set, output any text through the stream, pipe, or file. Null by default
ShowOnlyStackWithSource
Boolean
Since ASP .NET stack traces can get quite deep, setting to true will only show stack items with source code. False by default
LaunchDebuggerOnAssert
Boolean
If true, will launch or attach debugger whenever an assertion is triggered. True by default
Figure 5 HandleOutput and IsRequestFromLocalMachine
/// <summary>
/// Takes care of all the output for the trace or assertion.
/// </summary>
/// <param name="Message">
/// The message for the assertion or trace.
/// </param>
/// <param name="DetailMessage">
/// The optional detail message for an assertion.
/// </param>
/// <param name="StackArray">
/// The stack walk information for the assertion.  If this is
/// not null, this function is called from an assertion.
/// Trace output sets this to null.
/// </param>
protected void HandleOutput ( String    Message       ,
                              String    DetailMessage ,
                              ArrayList StackArray     )
{
    // Create the StringBuilder to help me build the text
    // string for the output here.
    StringBuilder StrOut = new StringBuilder ( ) ;

    // If the StackArray is not null, it's an assertion.
    if ( null != StackArray )
    {
        StrOut.Append ( Border ) ;
        StrOut.Append ( AssertionMsg ) ;
        StrOut.Append ( Border ) ;
    }

    // Pop on the message.
    StrOut.Append ( Message ) ;
    StrOut.Append ( CrLf ) ;

    // Poke on the detail message if it's there.
    if ( null != DetailMessage )
    {
        StrOut.Append ( DetailMessage ) ;
        StrOut.Append ( CrLf ) ;
    }

    // If an assertion, show the stack below a border.
    if ( null != StackArray )
    {
        StrOut.Append ( Border ) ;
    }

    // Go through and poke on all the stack information
    // if it's present.
    if ( null != StackArray )
    {
        foreach ( StackInfoItem StkItem in StackArray )
        {
            // The function name is always there so plop it on.
            StrOut.Append ( StkItem.FunctionString ) ;
            StrOut.Append ( CrLf ) ;
            if ( "" != StkItem.SourceString )
            {
                // Poke in a few spaces to indent the source
                // info.
                StrOut.Append ( "     " ) ;
                StrOut.Append ( StkItem.SourceString ) ;
                StrOut.Append ( CrLf ) ;
            }
        }
    }

    // Since I use the string multiple places, get it once here.
    String FinalString = StrOut.ToString ( ) ;

    if ( ( true == m_ShowDebugLog           ) &&
        ( true == Debugger.IsLogging ( ) )    )
    {
        Debugger.Log ( 0 , null , FinalString ) ;
    }
    if ( true == m_ShowOutputDebugString )
    {
        OutputDebugStringA ( FinalString ) ;
    }
    if ( null != m_EvtLog )
    {
        m_EvtLog.WriteEntry ( FinalString ,
                  System.Diagnostics.EventLogEntryType.Error ) ;
    }
    if ( null != m_Writer )
    {
        m_Writer.WriteLine ( FinalString ) ;
        // Add a CRLF just in case.
        m_Writer.WriteLine ( "" ) ;
        m_Writer.Flush ( ) ;
    }

    // Always do the page level output!
    if ( null != StackArray )
    {
        // Do the warning output to the current TraceContext.
        HttpContext.Current.Trace.Warn ( FinalString ) ;
        
        // Hunt down the AssertionControl on the page.
        
        // First, make sure the handler is a page!
        if ( HttpContext.Current.Handler is System.Web.UI.Page )
        {
            System.Web.UI.Page CurrPage = 
               (System.Web.UI.Page)HttpContext.Current.Handler ;
               
            // Take the easy way out if there are no 
            // controls (which I doubt!)                       
            if ( true == CurrPage.HasControls( ) )
            {
                // Hunt down the control.
                AssertControl AssertCtl = null ;
                FindAssertControl ( CurrPage.Controls , 
                                    out AssertCtl      ) ;
                                    
                // If there was one, add the happy assertion!                                            
                if ( null != AssertCtl )
                {
                    AssertCtl.AddAssertion ( Message       , 
                                             DetailMessage ,
                                             StackArray     ) ;
                }                                                     
            }                                             
        }

        // Finally, launch the debugger if I'm supposed to.
        if ( true == m_LaunchDebuggerOnAssert )
        {
            // If a debugger is already attached, I can
            // just use Debugger.Break on it.  It doesn't
            // matter where the debugger is running, as
            // long as it's running on this process.
            if ( true == Debugger.IsAttached )
            {
                Debugger.Break ( ) ;
            }
            else
            {
                // A debugger is not running.  I'll check
                // if the request is coming from the local
                // machine (i.e., the browser making the
                // request is on the machine serving the
                // request).  If so, I can safely
                // launch a debugger as someone is
                // physically logged into the machine.
                bool bRet = IsRequestFromLocalMachine ( ) ;
                if ( true == bRet )
                {
                    Debugger.Launch ( ) ;
                }
            }
        }
    }
    else
    {
        // The TraceContext is accessable right off the
        // HttpContext.
        HttpContext.Current.Trace.Write ( FinalString ) ;
    }
}

/// <summary>
/// Determines if the request came from a local machine.
/// </summary>
/// <returns>
/// Returns true if the request came from the local machine,
/// false otherwise.
/// </returns>
private bool IsRequestFromLocalMachine ( )
{
    // Get the request object.
    HttpRequest Req = HttpContext.Current.Request ;

    // Is the user sitting on the loopback node?
    bool bRet = Req.UserHostAddress.Equals ( "127.0.0.1" ) ;
    if ( false == bRet )
    {
        // Get the local IP address out of the server
        // variables.
        String LocalStr =
                      Req.ServerVariables.Get ( "LOCAL_ADDR" ) ;
        // Compare the local IP with the IP address that
        // accompanied the request.
        bRet = Req.UserHostAddress.Equals ( LocalStr ) ;
    }
    return ( bRet ) ;

}
Page view tracker