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>