Routing the Trace messages that are contained in the AuthorClass business object to the ASP.NET trace output can show you when the AuthorClass is created and manipulated during request processing and give you a more holistic view of what your application is doing. To route Trace messages to an ASP.NET Web page, you must add a WebPageTraceListener object. You can add a trace listener either through configuration (recommended) or programmatically. For more information about adding a trace listener through configuration, see <listeners> Element for <trace>.
For the next procedure, you will need a Web.config file. If you already have a Web.config file, skip the next procedure and go to the following one, in which you will add a WebPageTraceListener object.
To create a Web.config file
Open a text editor, such as Notepad, and create a new file.
Enter the following text into the file.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Save the file in the same directory as Default.aspx as Web.config.
Next, you will add a WebPageTraceListener object to your application to route Trace messages to the ASP.NET tracing output.
To add a WebPageTraceListener for your application through configuration
Open the Web.config file for your application.
Place the following code in your Web.config file after the <system.web> section.
<system.diagnostics>
<trace>
<listeners>
<add name="WebPageTraceListener"
type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</listeners>
</trace>
</system.diagnostics>
This configuration section adds a WebPageTraceListener object to the trace listeners collection for your application.
Adding a WebPageTraceListener using the configuration file for your application is the recommended method of adding a trace listener. However, you can also add the trace listener programmatically, which is especially useful, if you are using complex tracing logic and different types of trace listeners to trace your application, and the WebPageTraceListener is just one of many.
If you want to add a trace listener to your application programmatically, create a Global.asax file and add the listener by following these steps.
To create a Global.asax file and add a trace listener programmatically
Open a text editor, such as Notepad, and create a new file.
Enter following code into the file.
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web" %>
<script runat="server">
Sub Application_Start(sender As Object, e As EventArgs)
' Code that runs when the application is started.
End Sub
Sub Application_End(sender As Object, e As EventArgs)
' Code that runs when the application shuts down.
End Sub
</script>
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web" %>
<script runat="server">
void Application_Start(Object sender, EventArgs e) {
// Code that runs when the application is started.
}
void Application_End(Object sender, EventArgs e) {
// Code that runs when the application shuts down.
}
</script>
In your application root directory, save the file as Global.asax.
This creates a basic Global.asax, which is a compiled module that contains code that runs whenever the application is started or stopped.
Enter the following code into the Application_Start method of your Global.asax file.
' Create a trace listener for Web forms.
Dim gbTraceListener As New WebPageTraceListener()
' Add the event log trace listener to the collection.
System.Diagnostics.Trace.Listeners.Add(gbTraceListener)
// Create a trace listener for Web forms.
WebPageTraceListener gbTraceListener = new WebPageTraceListener();
// Add the event log trace listener to the collection.
System.Diagnostics.Trace.Listeners.Add(gbTraceListener);
This programmatically adds a WebPageTraceListener to the trace listeners collection for your application.
In the same directory as Default.aspx, save the file as Global.aspx.
With these steps, you have added a WebPageTraceListener to the trace listeners collection for the application. Although ASP.NET displays trace messages whenever tracing is enabled for a page, System.Diagnostics trace messages are written only when the code in which the trace messages reside is compiled by using an explicit compiler switch—the TRACE switch. In other words, if you do not explicitly compile the AuthorClass using the TRACE switch, you will not see the trace messages, even with the WebPageTraceListener added.
You can configure your application to automatically compile using the TRACE switch, by adding a new section to your Web.config file.
To compile your application automatically with trace enabled
Open the Web.config file for your application.
Place the following code into your Web.config file after the <system.diagnostics> section.
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp"
extension=".cs"
compilerOptions="/d:TRACE"
type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="1" />
<compiler language="VB"
extension=".vb"
compilerOptions="/d:Trace=true"
type="Microsoft.VisualBasic.VBCodeProvider, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</compilers>
</system.codedom>
By default, this configuration section makes your application compile all modules with the TRACE option.
If you view the Default.aspx page now, you will see both ASP.NET and System.Diagnostics trace messages. If the ASP.NET and System.Diagnostics trace messages do not appear, you might have to recompile the AuthorClass object.
From the trace information, you can see the AuthorClass object is created and its GetAuthors method is called during the PreRender method of the Web page life cycle. You can also see that there is a significant time lag when the AuthorClass object is created; the difference in time between the "Begin PreRender" message and the "AuthorClass is created." message is significant. This is because the AuthorClass opens the Authors.xml file and initializes its DataSet during creation, which is a relatively costly operation.
In the next section, you will learn how to configure your ASP.NET Web application to route all trace messages to the .NET Framework tracing output.