ASP.NET 2.0 introduced new attribute to Web.config <trace> element that allows you to route messages emitted by ASP.NET tracing to System.Diagnostics.Trace:writeToDiagnosticsTrace.
< trace enabled =”true” requestLimit =”20″ writeToDiagnosticsTrace =”true ” pageOutput =”true”/>
When you set writeToDiagnosticsTrace to true, all calls to System.Web.UI.Page.Trace.Write(the ASP.NET TraceContent) also go to System.Diagnostics.Trace.Write, enabling you to use all the standard TraceListeners. The simple writeToDiagnosticsTrace setting connects the ASP.NET tracing functionality with the rest of the base class library.
New Trace Listeners in ASP.NET 2.0
The new ASP.NET 2.0 WebPageTraceListener derives from System.Diagnostics.TraceListener and automatically forwards tracing information from any component calls to System.Diagnostics.Trace.Write. This enables you to write your components using the most generic trace provider and to see its tracing output in the context of your ASP.NET application.
The WebPageTraceListener is added to the web.config as shown below.
< system.diagnostics> <trace autoflush =”false” indentsize =”4″> <listeners> <add name=”webListeners” type=”System.Web.WebPageTraceListener, System.Web” /> </listeners> </trace>
This walkthrough illustrated how to work with ASP.NET and System.Diagnostics tracing features to route all trace messages a single output. You might want to experiment more with trace listeners and output and ASP.NET instrumentation features. For example, you might want to do the following:
There are several ways to find errors in an ASP.NET Web page. This includes using the debugger and tracing. Tracing displays messages about the actions that occur during page processing, and can include information that you choose to display. Tracing also gives you information about the data that is being exchanged by the server and browser. The data exchange includes control details, server variables, user name, cookies, and so on. This walkthrough illustrates how to use tracing.
Visual Studio provides you with tools to help track down errors in your ASP.NET Web pages. In this walkthrough, you will work with the debugger, which allows you to step through the page’s code line by line and examine the values of variables.
In the walkthrough, you will create a Web page that contains a simple calculator that squares a number. After creating the page (which will include a deliberate error), you will use the debugger to examine the page as it is running.
The Visual Studio debugger has the ability to attach to a process that is running outside of Visual Studio. You can use this attach capability to do the following:
- Debug an application that was not created in Visual Studio.
- Debug multiple processes simultaneously. You can also debug multiple processes by starting multiple projects within a single solution.
- Debug a process running on a remote computer.
- Debug a DLL that runs in a separate process that cannot easily be started from Visual Studio, for example, a service or an ISAPI DLL running with Internet Information Services.
- Start the debugger automatically when a process crashes while running outside of Visual Studio. This is Just-In-Time debugging.
You can control whether tracing is enabled or disabled for individual pages. If tracing is enabled, when the page is requested, ASP.NET appends to the page a series of tables containing execution details about the page request. Tracing is disabled by default.
Instead of enabling tracing for individual pages, you can enable it for your entire application. In that case, every page in your application displays trace information. Application tracing is useful when you are developing an application because you can easily enable it and disable it without editing individual pages. When your application is complete, you can turn off tracing for all pages at once.
When you enable tracing for an application, ASP.NET collects trace information for each request to the application, up to the maximum number of requests you specify. The default number of requests is 10. You can view trace information with the trace viewer.
By default, when the trace viewer reaches its request limit, the application stops storing trace requests. However, you can configure application-level tracing to always store the most recent tracing data, discarding the oldest data when the maximum number of requests is reached. For more information, see Application-Level Tracing Overview.
If you have enabled tracing for your application, when any page is requested, the page gathers trace information and executes any trace statements that it contains. You can view the trace output in the trace viewer. The trace viewer enables you to choose a specific request from the pages that have been requested from your application.
|When you enable tracing for an application, you can display trace output in any page in the application by setting the pageOutput attribute of the trace element to true in the Web.config file.
To view trace details for a specific request
Navigate to Trace.axd in the root of your application.
For example, if the URL for your application is http://localhost/SampleApplication, navigate to http://localhost/SampleApplication/trace.axd to view the trace information for that application.
Select the View Details link for the request that you want to investigate.
To enable tracing for the page
- Open the FirstWebPage.aspx page and switch to Design view.
- In Properties, in the list, click DOCUMENT.This displays properties for the page.
- Set Trace to true.The trace setting is actually made as part of the @ Page directive. You can see this by switching to Source view and looking at the first line in the page. The @ Page directive will look similar to the following:
<%@ Page language="C#" Trace="true" %>
To view tracing output in a separate browser window
Press CTRL+F5 to run the page.
Notice that the page no longer displays trace output. The page looks the way a user would see it.
In the box, enter a name, and then click Submit to confirm that the page works correctly.
Open a new browser window.
In the browser, in the Address box, type the URL of your site, substituting trace.axd for the name of the page that you are working with.
For example, if the URL for the page is the following: