Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows XP

Microsoft ASP.NET 4 : Diagnostics and Debugging - Application Tracing

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
6/28/2011 4:22:16 PM
Although single page tracing is useful (especially for quick spot checks for problems), it has a major downside in that it litters the page with lots of garbage at the end. You can use application tracing to get around that. Application tracing shows you exactly the same details as page tracing, except they're held in memory and made available rendered as a different page and through a special HTTP handler that ASP.NET provides.

To turn on tracing, you need to enable tracing in web.config like so:

<configuration>
<system.web>
<trace enabled="true"/>
</system.web>
</configuration>

This simply turns on tracing. You can actually control several aspects of page tracing. For example, you could have tracing available on the host computer only (in case you don't want clients getting to your trace information). You might also want to control the number of responses that are held in memory.

Table 1 shows the possible values that can go in the configuration file to support tracing.

Table 1. Web.config Settings Supporting Tracing
KeyPossible ValuesMeaning
Enabledtrue falseEnable or disable application-level tracing.
localOnlytrue falseSpecify whether to show trace output only on local host or everywhere.
mostRecenttrue falseSpecify whether to recycle traces once requestLimit is met or to keep the first N (up to the requestLimit threshold).
pageOutputtrue falseSpecify whether to display trace output on individual pages in addition to caching application-level traces.
requestLimitDecimal numberSpecify how many traces to store in memory before removing earlier traces (default is 10).
traceModeSortByTimeSortByCategory Specify the order in which to display trace information.
writeToDiagnosticsTracetrue falseSpecify whether the trace data is also piped to System.Diagnostics.Trace.

The following exercise demonstrates how application-level tracing works and how to navigate around the results.

Using application-level tracing

  1. Open the DebugORama project. Open the TraceMe.aspx page. Turn off tracing in the page by ensuring the Page class's Trace property is set to false.

  2. Ensure that application-level tracing is turned on in web.config. That is, open web.config and add a trace element, as shown earlier. If the application doesn't yet have a configuration file, you can add one by selecting Add New Item from the local project menu.

  3. Surf to the TraceMe.aspx page a few times by pressing Ctrl+F5, and then adding a few strings to the table.

  4. In the URL that appears in the address box, make the endpoint Trace.axd, as shown in the following graphic. This name in the URL redirects request processing through a special handler that renders the tracing results being kept in memory.



  5. You should be able to see a list of requests. You can see individual requests and the request details, as shown in the following graphic, by clicking the View Details link.



    Notice how the output is exactly the same as the output of the earlier page tracing example. However, now the tracing information stands alone without cluttering up the Web page.

1. Enabling Tracing Programmatically

Although much of the time you will enable tracing through the Designer, at times you might find it useful to manage tracing during run time (programmatically). For example, you might have regular clients receive normal content; however, when someone with specific credentials appears, you might want to enable tracing for that individual. You might also decide to modify tracing when a certain parameter comes through the request.

The DebugORama site includes a page named EnableTracing.aspx that illustrates how to control the tracing programmatically. If the user types the correct password, the tracing is turned on. The page also shows how to enable and disable tracing programmatically.

public partial class EnableTracing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{


if (this.TextBoxSecretCode.Text == "password")
{
this.Trace.IsEnabled = true;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
this.Trace.IsEnabled = false;
}
}

2. The TraceFinished Event

The tracing context includes an interesting event named TraceFinished that gives you a last chance opportunity to log the tracing information or deal with it in some other way. The TraceFinished event is raised by the Trace object after all request information is gathered.

To subscribe to the event, simply set up the handler during the Page_Load event. The DebugORama example includes a page named TraceFinished.aspx that shows how the trace information was gathered and written to the debug console using System.Diagnostics.Debug.

public partial class TraceFinished : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Trace.TraceFinished +=
new TraceContextEventHandler(TracingFinished);
}

void TracingFinished(object sender, TraceContextEventArgs e)
{
foreach (TraceContextRecord traceContextRecord in e.TraceRecords)
{
System.Diagnostics.Debug.WriteLine(traceContextRecord.Message);
}
}
}

3. Piping Other Trace Messages

In the last example, tracing messages were logged manually to the debug console by setting up the TraceFinished event handler in the Trace context. System.Diagnostics.Debug is a standard .NET type that's helpful for managing tracing and debugging information. Since version 2.0, ASP.NET has had the ability to plug in the WebPageTraceListener type so that calls to System.Diagnostics.Trace are also inserted into the ASP.NET trace. Setting it up is simply a matter of inserting a line in web.config (note the writeToDiagnosticsTrace option in Table 1). A case in which this is useful is for logging compiler output. To do this, set the writeToDiagnosticsTrace option to true, and then turn on compiler tracing. Compiler tracing is another setting you can set in web.config, but notice that this lies outside the typical System.web section of web.config.

<system.codedom>
<compilers>
<compiler compilerOptions="/d:TRACE" />
</compilers>
</system.codedom>
Other -----------------
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Page Tracing
- Microsoft ASP.NET 4 : Caching and State Management - The Wizard Control: An Alternative to Session State
- Microsoft ASP.NET 4 : Caching and State Management - Tracking Session State
- Microsoft ASP.NET 4 : Caching and State Management - Configuring Session State
- Microsoft ASP.NET 4 : Caching and State Management - Session State and More Complex Data
- Microsoft ASP.NET 4 : Caching and State Management - Introduction to Session State
- Installing and Configuring a Modem : Modifying the Modem’s Advanced Properties
- Installing and Configuring a Modem : Modifying the Modem’s General Properties
- Installing and Configuring a Modem : Working with Different Dialing Locations
- Installing and Configuring a Modem : Installing a Modem & Testing the Modem
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer