asp.net.ph

Skip Navigation LinksASP.NET Applications > ASP.NET Optimization > ASP.NET Trace Functionality > Page-level Tracing > Writing to the Trace Log

Writing to the Trace Log

ASP.NET Web Applications   ASP.NET Trace Functionality


ASP.NET includes a built-in Trace object that allows you to write debug statements that are diplayed when the object is enabled for a page or the entire application.

ASP.NET uses the TraceContext class to store information about a request, its control hierarchy, and trace information, which includes the lifecycle stages of a page request along with any custom statements you choose to include. The TraceContext class is available through the Page.Trace property.

The TraceContext class provides two methods that allow you to write statements to the trace log, Write and Warn. Each method is overloaded and allows you to specify a tracing category, a text message, and optional error information. The only difference between these methods is that the Warn method displays in red when you view trace information.

These trace statements, and the rest of the tracing information, is displayed only when you enable tracing for the page or the entire application. The below code shows how tracing is enabled for a page.

<%@ Page Language = "C#" Trace = "True" %>
  C# VB

To enable tracing for an entire application, see Enabling Application-level Tracing.

The following example demonstrates how to use trace statements when developing a Web Forms page. It overrides the Load method, writing a trace statement with a category of sampleTrace and a message of "This is a sample trace statement.".

<%@ Page Language = "C#" Trace = "True" %>
<html>
   <script runat = "server">
      protected void Page_Load ( )
         Trace.Write ( "sampleTrace","This is a sample trace statement." );
      }
   </script> 
</html>
  C# VB

Since tracing is disabled by default, you do not need to remove trace statements from your code prior to publishing your page or application on the Internet or an intranet. While these messages exist in the code, you must enable tracing on the page before trace statements or the trace stack will be displayed by a requesting browser. For more information, see Displaying Trace Messages on the Page.

See Also

Page-level Tracing   Displaying Trace Messages on the Page



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note