日期:2010-11-09  浏览次数:20449 次

  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>