共用方式為


TraceContextEventHandler 代理人

定義

表示處理 TraceFinished 物件之 TraceContext 事件的方法。

public delegate void TraceContextEventHandler(System::Object ^ sender, TraceContextEventArgs ^ e);
public delegate void TraceContextEventHandler(object sender, TraceContextEventArgs e);
type TraceContextEventHandler = delegate of obj * TraceContextEventArgs -> unit
Public Delegate Sub TraceContextEventHandler(sender As Object, e As TraceContextEventArgs)

參數

sender
Object

事件來源 (TraceContext)。

e
TraceContextEventArgs

TraceContextEventArgs,其中包含事件資料。

範例

下列程式代碼範例示範如何註冊 TraceContextEventHandler 委派來處理 TraceFinished 事件。 在此範例中 OnTraceFinished ,方法會逐一查看追蹤記錄,並將其寫入響應數據流。

<%@ 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>

備註

建立 TraceContextEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。

如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱