Επεξεργασία

Κοινή χρήση μέσω


TraceContextEventHandler Delegate

Definition

Represents the method that handles the TraceFinished event of a TraceContext object.

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)

Parameters

sender
Object

The event source (the TraceContext).

e
TraceContextEventArgs

A TraceContextEventArgs that contains the event data.

Examples

The following code example demonstrates how to register a TraceContextEventHandler delegate to handle the TraceFinished event. In this example, the OnTraceFinished method iterates through the trace records and writes them to the response stream.

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

Remarks

When you create a TraceContextEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

For more information about event handler delegates, see Handling and Raising Events.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

See also