ASP+ provides a powerful trace mode feature that enables both server control and page developers to include instrumentation messages throughout their code. These instrumentation messages can be used to simplify the process of identifying exactly what occurs during a given Web request, and to help locate resulting problems.
Writing to the Trace Log
Control and page developers can write to the trace log using the Trace property exposed on the Control base class. Developers can organize each output message using a provided category string.
Sample Control Using Tracing
public class MyButton : Control { public override void Render(HTMLTextWriter output) { Trace.Write("Render", "Button class is about to render…."); output.Write("<button>Simple Button</button>"); Trace.Write("Render", "Button class has just finished rendering….); } }
Sample Page Using Tracing
<%@ Page Language="VB" Trace=True %> <html> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs) Trace.Write("We’re at Load()!!!") End Sub </script> <body> <form runat=server> <span id="Message" runat=server/> </form> </body> </html>
Output Trace Messages
By default, tracing is not enabled on a page. This means that Trace.IsEnabled will return false and no messages written to the trace log will be output during a Web request.
You can optionally enable tracing by setting the Trace attribute on the Page directive to true. For example:
<%@ Page Language="VB" Trace=True %>
In addition to causing the Trace.IsEnabled property to return true, this will cause the Page class to automatically output an HTML table at the conclusion of page rendering that details all the assorted category trace logs. For example:
<html> <body> <form runat=server> <span id="Message" runat=server/> </form> </body> </html> <table> <tr> <td colspan=2><h1>Trace Log</h1></td> </tr> <tr> <td><h3>Dev1 Category</h3></td> <td> We’re at Load()!!! </td> </tr> <tr> <td><h3>Render Category</h3></td> <td> Button class is about to render…. <br> Button class has just finished rendering…. </td> </tr> </table>
You can optionally toggle the order/presentation of trace messages using the tracemode attribute exposed on the Page directive. A value of SortByTime will cause the trace messages to be output in the order in which they were written. A value of SortByCategory will cause the trace messages to be output alphabetically by category. A value of ShowInline will cause the messages to be emitted immediately after Trace.Write is called.
Note that in addition to outputting user-provided trace content, the page class will also automatically include additional performance data, tree structure information, and state management content.