ASP.NET 2.0 allows code to access per-request tracing information in the TraceFinished event. The TraceFinished event is raised by the TraceContext object and is used to expose trace messages after collecting all request information. This event can be used to output trace information differently. The following code uses a program to output tracking information.
C#:
<%@ Page Language="C#" %>
<SCRIPT runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
Trace.TraceFinished += new TraceContextEventHandler(OnTraceFinished);
Trace.Write("Custom category", "Custom trace message");
}
private void OnTraceFinished(object sender, TraceContextEventArgs e)
{
foreach (TraceContextRecord tcr in e.TraceRecords)
{
Response.Write(string.Format(" <li>Tracking Category: {0} Tracing Message: {1}", tcr.Category, tcr.Message));
}
}
</SCRIPT>
VB.NET:
<%@ Page Language="VB"%>
<SCRIPT runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
AddHandler Trace.TraceFinished, AddressOf OnTraceFinished
Trace.Write("Custom Category", "Custom Trace Message")
End Sub
Private Sub OnTraceFinished(ByVal sender As Object, ByVal e As TraceContextEventArgs)
Dim tcr As TraceContextRecord
For Each tcr In e.TraceRecords
Response.Write(String.Format(" <li>Tracking Category: {0} Tracing Message: {1}", tcr.Category, tcr.Message))
Next
End Sub
</SCRIPT>