TraceContextEventArgs.TraceRecords 属性

定义

获取与当前请求关联的 TraceContextRecord 消息的集合。

public:
 property System::Collections::ICollection ^ TraceRecords { System::Collections::ICollection ^ get(); };
public System.Collections.ICollection TraceRecords { get; }
member this.TraceRecords : System.Collections.ICollection
Public ReadOnly Property TraceRecords As ICollection

属性值

与当前请求关联的跟踪记录的集合。

示例

下面的代码示例演示如何注册 TraceContextEventHandler 委托来处理 TraceFinished 事件。 在此示例中,OnTraceFinished方法通过 TraceRecords 属性访问对象的集合TraceContextRecord,循环访问对象集合,并将其写入响应流。

<%@ Page language="c#" Trace="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
    // Register a handler for the TraceFinished event.
    Trace.TraceFinished += new 
        TraceContextEventHandler(this.OnTraceFinished);

    // Write a trace message.
    Trace.Write("Web Forms Infrastructure Methods", "USERMESSAGE: Page_Load complete.");
}
 
// A TraceContextEventHandler for the TraceFinished event.
void OnTraceFinished(object sender, TraceContextEventArgs e)
{
    TraceContextRecord r = null;    
    
    // Iterate through the collection of trace records and write 
    // them to the response stream.
    foreach(object o in e.TraceRecords)
    {
        r = (TraceContextRecord)o;
        Response.Write(String.Format("trace message: {0} <BR>", r.Message));
    }
}       
</script>
<%@ Page language="VB" Trace="true" %>
<script runat="server">
' The Page_Load method.
Private Sub Page_Load(sender As Object, e As EventArgs)

    ' Register a handler for the TraceFinished event.
    AddHandler Trace.TraceFinished, AddressOf OnTraceFinished

    ' Write a trace message.
    Trace.Write("Web Forms Infrastructure Methods", "USERMESSAGE: Page_Load complete.")
End Sub ' Page_Load
 
' A TraceContextEventHandler for the TraceFinished event.
Private Sub OnTraceFinished(sender As Object, e As TraceContextEventArgs)

    Dim r As TraceContextRecord
    Dim o As Object
    
    ' Iterate through the collection of trace records and write 
    ' them to the response stream.

    For Each o In e.TraceRecords
        r = CType(o, TraceContextRecord)
        Response.Write(String.Format("trace message: {0} <BR>", r.Message))
    Next

End Sub ' OnTraceFinished
</script>

注解

每次调用 TraceContext.WriteTraceContext.Warn 方法都会生成一个 TraceContextRecord 对象,该对象将添加到消息集合中 TraceRecords 。 方法 Warn 调用将 IsWarning 属性设置为 true,而其他方法调用将其设置为 false

可以循环访问消息并 TraceRecords 检查具有特定类别或消息的记录,以及它们是否为警告。

适用于

另请参阅