ASP.NET 2.0允許透過程式碼在TraceFinished事件裡存取每個請求的追蹤資訊。 TraceFinished事件是由TraceContext物件引發,用於在收集所有請求資訊後公開追蹤訊息。可以利用此事件對追蹤資訊進行不同的輸出。以下程式碼就是用程式輸出追蹤資訊。
C#:
<%@ Page Language="C#" %>
<SCRIPT runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
Trace.TraceFinished += new TraceContextEventHandler(OnTraceFinished);
Trace.Write("自訂類別", "自訂追蹤訊息");
}
private void OnTraceFinished(object sender, TraceContextEventArgs e)
{
foreach (TraceContextRecord tcr in e.TraceRecords)
{
Response.Write(string.Format(" <li>追蹤類別:{0} 追蹤訊息:{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("自訂類別", "自訂追蹤訊息")
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>追蹤類別:{0} 追蹤訊息:{1}", tcr.Category, tcr.Message))
Next
End Sub
</SCRIPT>