Microsoft KB Archive/327852

= HOW TO: Get Trace.Write Statements in Regular Code to Appear in Page Logs by Using Visual Basic .NET =

Article ID: 327852

Article Last Modified on 5/21/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q327852



For a Microsoft Visual C# .NET version of this article, see 327848.

IN THIS TASK
SUMMARY
 * Create the ASP.NET Application
 * Trace from a Class File
 * Enable Trace for the Application

REFERENCES



SUMMARY
There are two Trace classes in the Microsoft .NET Framework library: System.Diagnostics.Trace and System.Web.TraceContext. These Trace classes are frequently confused with each other. However, each one is used for a specific purpose.

You typically use the System.Diagnostics.Trace class to debug console applications or Windows Forms applications. You use System.Web.TraceContext only in Microsoft ASP.NET applications. To display Trace.Write statements on an ASP.NET page from a Class Library, you must use the System.Web namespace. The following examples describe how to use Trace.Write from a Class Library in an ASP.NET application.

back to the top

Create the ASP.NET Application
By default, Trace is not enabled in an ASP.NET application. You can enable Trace either at the page level or at the application level. The following steps describe how to create an ASP.NET application, and then how to enable Trace at the page level.  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates. In the Location box, type the following URL to name the project: http://localhost/VBNETTrace. Click OK. The New Project dialog box closes, and an ASP.NET application is created. The design editor loads with WebForm1.aspx loaded in the Design window.

  To enable page level Trace for WebForm1.aspx, point to View, and then click HTML Source. Locate the following line of code: <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;VBNETTrace.WebForm1&quot;%> Add Trace=&quot;True&quot;, so that the code reads: <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;VBNETTrace.WebForm1&quot; Trace=&quot;True&quot;%> Trace is now enabled only for WebForm1.aspx.

 To save WebForm1.aspx, click File, and then click Save.</ol>

back to the top

Trace from a Class File
With Trace enabled, follow these steps to create a class and then to execute Trace commands from that class. <ol> To add a class file, in Solution Explorer, right-click VBNETTrace, point to Add, and then click Add Class. The Add New Item dialog box appears.</li> In the Name box, accept the default Class1.vb. Click Open. The Add New Item dialog box closes, and then Class1.vb opens in the editor window.</li>  Add the following directive at the top of Class1.vb: Imports System.Web Add the following code to Public Class1: Public Function Add(ByVal a As Int32, ByVal b As Int32) As Int32 Dim cur As HttpContext = HttpContext.Current If Not cur Is Nothing Then cur.Trace.Write(&quot;The value for a is&quot;, a.ToString) +     cur.Trace.Write(&quot;The value for b is&quot;, b.ToString) End If   End Function This method first accesses HttpContext class and then verifies that the context is not Nothing. When you gain access to the HttpContext class, you can call Trace class output when you call the cur.Trace.Write method. </li> To save the file, click Save on the File menu.</li> To test and to use the Add method, follow these steps: <ol style="list-style-type: lower-alpha;"> Right-click WebForm1.aspx in Solution Explorer, and then click View Code.</li>  In the Page Load event, add the following lines of code: Dim clsAdd As Class1 = New Class1 Dim answer As Int32 = clsAdd.Add(1, 2) </li></ol> </li> To build the page, right-click WebForm1.aspx in Solution Explorer, and then click Build.</li> To view the page, right-click WebForm1.aspx in Solution Explorer, and then click Browse. Notice that WebForm1.aspx opens in the browser with Trace enabled. Under the Trace Information section, notice that the following lines of code are added:

<pre class="fixed_text">The value for a is 1 The value for b is 2

</li></ol>

back to the top

Enable Trace for the Application
To enable Trace for the whole application (and not just for a single page), you must enable Trace in the Web.config file. Follow these steps to enable Trace on an application-wide basis. <ol> In Solution Explorer, right-click Web.config, and then click Open to edit the Web.config file.</li>  Locate the following code: <trace enabled=&quot;false&quot; requestLimit=&quot;10&quot; pageOutput=&quot;false&quot; traceMode=&quot;SortByTime&quot; localOnly=&quot;true&quot; /> and modify it so that it reads: <trace enabled=&quot;true&quot; requestLimit=&quot;10&quot; pageOutput=&quot;true&quot; traceMode=&quot;SortByTime&quot; localOnly=&quot;true&quot; />                   </li></ol>

back to the top

<div class="references_section">