Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

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.
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click ASP.NET Web Application under Templates.
  4. In the Location box, type the following URL to name the project: http://localhost/CSharpTrace. 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.
  5. To enable page level Trace for WebForm1.aspx, point to View, and then click HTML Source. Locate the following line of code:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CSharpTrace.WebForm1" %>
    					
    Add Trace="True", so that the code reads:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CSharpTrace.WebForm1" Trace="True" %>
    					
    Trace is now enabled only for WebForm1.aspx.
  6. To save WebForm1.aspx, click File, and then click Save.

Trace from a Class File

With Trace enabled, follow these steps to create a class and then to execute Trace commands from that class.
  1. To add a class file, in Solution Explorer, right-click CSharpTrace, point to Add, and then click Add Class. The Add New Item dialog box appears.
  2. In the Name box, accept the default Class1.cs. Click Open. The Add New Item dialog box closes, and then Class1.cs opens in the editor window.
  3. Add the following directive at the top of Class1.cs:
    using System.Web;
    						
    Add the following method to Class1:
    public int Add( int a, int b )
    {
    	HttpContext cur = HttpContext.Current;
    	if( cur != null )
    	{
    		cur.Trace.Write("The value for a is", a.ToString() );
    		cur.Trace.Write("The value for b is", b.ToString() );
    	}
    	return a + b;
    }
    						
    This method first accesses HttpContext class and then verifies that the context is not null. When you gain access to the HttpContext class, you can call Trace class output when you call the cur.Trace.Write method.
  4. To save the file, click Save on the File menu.
  5. To test and to use the Add method, follow these steps:
    1. Right-click WebForm1.aspx in Solution Explorer, and then click View Code.
    2. In the Page Load event, add the following lines of code:
      Class1 myClass = new Class1();
      int answer = myClass.Add( 1, 2 );
      						
  6. To build the page, right-click WebForm1.aspx in Solution Explorer, and then click Build.
  7. 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:
    The value for a is 1 
    The value for b is 2
    						

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.
  1. In Solution Explorer, right-click Web.config, and then click Open to edit the Web.config file.
  2. Locate the following code:
    <trace
            enabled="false"
            requestLimit="10"
            pageOutput="false"
            traceMode="SortByTime"
    		localOnly="true"
        />
    						
    and modify it so that it reads:
    <trace
            enabled="true"
            requestLimit="10"
            pageOutput="true"
            traceMode="SortByTime"
    		localOnly="true"
        />
    					

↑ Back to the top


Keywords: KB327848, kbhowtomaster, kbdebug, kbconfig

↑ Back to the top

Article Info
Article ID : 327848
Revision : 8
Created on : 5/21/2003
Published on : 5/21/2003
Exists online : False
Views : 327