How to Trace Web Applications in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Feb 11, 2015.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    32
    Trophy Points:
    0
    The ASP.NET applications helps user to track the execution of the program and ensure that the application is executing smoothly. The diagnostic information is available on the web page. The information contains execution time of the page methods, controls rendering, HTTP header, and session state. The custom tracing information can be displayed to the user.

    The tracing information can be written in the log file or text files through trace listeners.

    Implementing Tracing



    Tracing can be implemented using any of the following levels:
    1. Page Level
    2. Application Level
    1. Page Level Tracing

    It is used for creating diagnostic information at the end of the page rendering. The information about requests and their responses is added. The page problems are diagnosed. The reasons for the processing delay are recognized through tracing. The problems can be easily resolved once the delay is recognized.

    For enabling the page level tracing, the following steps are performed.
    1. Add the following directive at the top of the page.
      Code:
      <%@ Page Trace ="True" %>
      
    2. Add the TraceMode attribute in the @Page directive. User can sort the messages using the time or category.
    The following code demonstrates the sorting of trace messages on the basis of category.
    Code:
    <%@ Page Language = "C#" Trace = "True" TraceMode = "SortByCategory" %>
    
    The following output is displayed when user executes the application.

    [​IMG]

    The information displayed when the page level tracing is enabled is as shown below:
    1. Request details: It is used to display the information such as session ID, request time, response time, return status code.
    2. Trace information: It displays the performance related information of the page life cycle. The Message column displays the events. The Category column displays the category of the message.
    3. Control tree: The information about the control on the tree is displayed. The weight of the web page can be calculated using the control tree.
    4. Session State: The information about the session variables, types and values are displayed.
    5. Application State: The information about the application variables, types and values are displayed.
    6. Request Cookies Collection: It displays the information about the cookies sent to the web browser with the web page request.
    7. Response Cookies Collection: It displays the information about the cookies returned to the web server with the response.
    8. Headers Collection: It displays the information about all the headers that are sent to the web server as a request from the browser.
    9. Response Headers Collection: It displays the information about all the headers that are sent to the client as response from the web browser.
    10. Querystring collection: It displays the list of values included in the query string. The information can be viewed in the Web Page URI.
    11. Server variables: It displays the server variables and values at the web page request.
    2. Application Level Tracing

    In the Page level tracing, all the information is displayed at the end of the page. All viewers must view the information at the bottom of the page. If user wants to view the information for all the web pages, the page level tracing is used.

    Application level tracing traces information for every page in an application. The page as trace.xsd is used to view the information of tracing. The administrator can view this page. For using the xsd page, the application level tracing is enabled. The <trace> element is useful for changing the tracing settings of the website.

    The following code snippet demonstrates the application level tracing in the web.config file.
    Code:
    <configuration>
        <system.web>
            <trace
            enabled = "true"
            requestLimit = "100"
            pageOutput = "false"
            traceMode = "SortByTime"
            localOnly = "true"
            mostRecent = "false" />
        </system.web>
    </configuration>
    
    Where,

    enabled is used to indicate the tracing is enabled for the application. The default value is false.

    requestLimit is used for storing the information for the maximum number of HTTP requests.

    pageOutput is used for indicating the trace information displayed at the bottom of the web page.

    traceMode is order in which the trace messages are displayed. User can sort them by the values as SortByTime and SortByCategory.

    localOnly is used to state whether the tracing information to be displayed on the local computer only

    mostRecent is used for storing the most recent trace messages is set to true. When the information reaches the maximum limit the requestLimit attribute discards the older messages.

    Trace Information



    In an application, the Response.Write statements are used for displaying the debugging the information. The statements are removed once the application is ready for deployment.

    Tracing helps user to insert the debugging code within the application such that the code is not removed at run time of deployment. The Trace property of the Trace class is used for writing the custom trace statements.

    The properties and methods provided by the trace objects are as mentioned below:
    1. IsEnabled: It shows whether the tracing is enabled for the request.
    2. TraceMode: It sets the trace mode through the SortByTime or SortByCategory
    3. Warn: The trace information is displayed in red color.
    4. Write: The trace information is displayed in black color.
    The Trace object is used for writing the custom trace statements once the tracing is enabled. The Write and Warn methods of the TraceContext class are used for writing the trace statements to the trace log. The methods can be overloaded using different versions.

    The syntax for the overloaded methods are as mentioned below:
    Code:
    public void [ Warn | Write ] ( String message )
    public void [ Warn | Write ] ( String category, String message )
    public void [ Warn | Write ] ( String category , String message, Exception errorInfo )
    
    The arguments passed to the methods are as mentioned below:
    1. message: It refers to the message displayed in the Trace information section
    2. category: It refers to the arbitrary string used for grouping similar messages.
    3. errorInfo: It refers to an object of the Exception class containing the exception details.
    The trace listeners are used for collecting, storing and routing the tracing messages. The ASP.NET provides user with the predefined trace listeners.
    1. TextWriterTraceListener: It is used for redirecting the trace output to a file or console.
    2. EventLogTraceListener: It is used for redirecting the trace output to an event log.
    3. DefualtTraceListener: It is used to redirect the trace output to the function and method.
    For enabling the trace listener, the Listeners collection is added. All the listeners receive the similar output but differ in the output.

    Adding Trace Listener



    The <listeners> tag is collection of all the trace listeners. The trace listener is added to the config file within the <listeners> tag.

    The following code snippet is used to demonstrate the addition of trace listener.
    Code:
    <configuration>
        <system.diagnostics>
            <trace autoflush = "true" >
            <listeners>
            <add name = "Listener1" type="System.Diagnostics.TextWriterTraceListener" initializeData = "C:\listener1.txt" />
            </listeners>
            </trace>
        </system.diagnostics>
    </configuration>
    
    In the above code snippet, a listener , listener1 is added to the <Listeners> tag. The Listener writes the trace information to the txt file. The autoflash attribute of the trace element automatically flushes the output buffer.

    In System.Diagnostics listener, add the following code in the web.config file.
    Code:
    <system.web>
        <trace writeToDiagnosticsTrace = "true" />
        <customErrors mode = "off" />
    </system.web>
    

    Removal of Trace Listener



    To remove the trace listener, add the following code snippet in the configuration file.
    Code:
    <configuration>
        <system.diagnostics>
            <trace>
            <listeners>
            <remove name = "Listener1" />
            </listeners>
            </trace>
        </system.diagnostics>
    </configuration>
    
    In the above code snippet, trace listener, Listener1 is removed through the use of <remove> element.

    Trace Switches



    Trace switches helps user to enable, disable and filter tracing output even after the application is destroyed. The following switch types are provided by the .NET framework.
    1. SourceSwitch: It provides user with multilevel switch to control the tracing and debug output without the code compilation. The Level property is used to set or get the switch TraceLevel value.
    2. TraceSwitch: It helps user to set the level from where user can retrieve the statements. The type of tracing output can be controlled through the trace switch.
    3. BooleanSwitch: It acts as a toogle switch that helps user to enable and disable statements.
    For instantiating a TraceSwitch, create an object for it. The following code snippet demonstrates the initializing the object.
    Code:
    System.Diagnostics.TraceSwitch TraceSwitch1 = new System.Diagnostics.TraceSwitch ( "MySwitch", "Part of the application");
    
    In the above code, the object of the TraceSwitch class is created and initialized.

    The Trace switch can be configured at different levels in a web application. The levels help user to trace several types of messages such as error messages, informational messages, and detailed messages.

    The following list states the levels of TraceLevel enumeration.
    1. Off: It has an integer value 0. It states that no message is displayed.
    2. Error: It has an integer value 1. It states that error messages are displayed.
    3. Warning: It has an integer value 2. It states that warning and error messages are displayed.
    4. Info: It has an integer value 3. It states that informational, warning and error messages are displayed.
    5. Verbose: It has an integer value 4. It states that verbose, informational warning and error messages are displayed.
    The following code snippet demonstrates the configuration of the trace switch.
    Code:
    <system.diagnostics>
        <switches>
            <add name="Switch1" value ="2" />
        </switches>
    </system.diagnostics>
    
     
    Last edited by a moderator: Jan 21, 2017
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice