Recipe 13.2. Uncovering Page-Level Problems


Problem

You want to find the source of a problem that appears to be associated with a particular page of your application, such as a page that completes its operations more slowly than desired.

Solution

Enable page-level tracing for the page in question by setting the trace attribute of the @ Page directive in the .aspx file to "TRue" and then using TRace.Write (or TRace.Warn) statements as warranted in your code-behind to write trace information to the trace output.

Examples 13-1, 13-2 through 13-3 show the code we've written to illustrate this solution. Example 13-1 shows the .aspx file for a typical ASP.NET page. The code-behind class for the page appears in Examples 13-2 (VB) and 13-3 (C#). By running the page and analyzing the trace sequence, you can see how long certain key operations are taking. The output with the trace sequence is shown in Figure 13-1.

Discussion

Tracing tracks and presents the execution details about an HTTP request. The TraceContext class is where ASP.NET stores information about an HTTP request and its trace information. You access the TraceContext class through the Page.Trace property of an ASP.NET page. To enable tracing for the page, be sure to set the trace attribute of the @ Page directive in the .aspx file to "TRue", as shown in Example 13-1.

The traceContext class has two methods for writing statements into the trace log: Write and Warn. The only difference is that Warn outputs statements in red so they are easier to spot in the trace log. Both methods are overloaded and have three versions. If you pass a single string argument, ASP.NET will write it to the Message column of the trace log, as shown in Figure 13-1. If you use two string arguments, the first string will appear in the Category column and the second in the Message column. If you use a third argument, it will have to be of type Exception and contain information about an error, which ASP.NET then writes to the trace log.

If you've placed trace.Write or trace.Warn statements in your code, you will not have to worry about removing them later. The common language runtime (CLR) will ignore them when tracing is disabled. Disable page-level tracing before deploying your application to a production environment.

In our example, trace.Write is used three times to put custom messages into the trace sequence: the first time to mark the start of the concatenations and the second to mark the end of the concatenations. The third message outputs the average time for a string concatenation. The latter shows how inefficient it is to use a classic concatenation operator (& or +) in ASP.NET string operations. (See Recipe 19.2 for further discussion of this code as well as the advantages of using the StringBuilder object to build strings over the classic concatenation operators.)

Figure 13-1. Sample tracing output


In Figure 13-1 the trace log (beginning with "Request Details") appears below the standard output for the ASP.NET page enabled for trace. Here's an explanation of the "Trace Information" section contents in the trace log:


Category

A custom trace category that you specified as the first argument in a trace.Write (or trace.Warn) method call.


Message

A custom trace message that you specified as the second argument in a trace. Write (or TRace.Warn) method call.


From First (s)

The time, in seconds, since the request processing was started (a running total).


From Last (s)

The time, in seconds, since the last message was displayed. This column is especially helpful for seeing how long individual operations are taking.

See Also

Recipe 19.3

Example 13-1. Page-level tracing (.aspx)

 <%@ Page Trace="True" Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH13TestPageLevelTracingVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH13TestPageLevelTracingVB" Title="Test Page Level Tracing" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Page-Level Tracing (VB) </div> </asp:Content> 

Example 13-2. For page-level tracing code-behind (.vb)

 Option Explicit On Option Strict On Imports System Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code-behind for ''' CH13TestPageLevelTracingVB.aspx ''' </summary> Partial Class CH13TestPageLevelTracingVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles Me.Load Const STRING_SECTION As String = "1234567890" Dim testStr As String Dim counter As Integer Dim startTime As DateTime Dim elapsedTime As TimeSpan Dim loops As Integer 'output trace message indicating the start of the concatenations Trace.Write("Page_Load", "Before performing concatenations") 'Measure the elapsed time for 10,000 classic string concatenations loops = 10000 startTime = DateTime.Now( ) testStr = "" For counter = 1 To loops testStr &= STRING_SECTION Next 'output trace message indicating the end of the concatenations Trace.Write("Page_Load", "After performing concatenations") 'calculate the elapsed time for the string concatenations elapsedTime = DateTime.Now.Subtract(startTime) 'Write average time per concatenation in milliseconds to trace sequence Trace.Write("Aver/concat", _ (elapsedTime.TotalMilliseconds / loops).ToString("0.0000")) End Sub 'Page_Load End Class 'CH13TestPageLevelTracingVB End Namespace 

Example 13-3. For page-level tracing code-behind (.cs)

 using System; using System.Configuration; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code-behind for /// CH13TestPageLevelTracingCS.aspx /// </summary> public partial class CH13TestPageLevelTracingCS : System.Web.UI.Page { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { const string STRING_SECTION = "1234567890"; string testStr = null; DateTime startTime; TimeSpan elapsedTime; int counter; int loops; // output trace message indicating the start of the concatenations Trace.Write("Page_Load", "Before performing concatenations"); // measure the elapsed time for 10000 classic string concatenations loops = 10000; startTime = DateTime.Now; testStr = ""; for (counter = 1; counter <= loops; counter++) { testStr += STRING_SECTION; } // output trace message indicating the end of the concatenations Trace.Write("Page_Load", "After performing concatenations"); // calculate the elapsed time for the string concatenations elapsedTime = DateTime.Now.Subtract(startTime); // Write average time per concatenation in milliseconds to trace sequence Trace.Write("Aver/concat", (elapsedTime.TotalMilliseconds / loops).ToString("0.0000")); } // Page_Load } // CH13TestPageLevelTracingCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net