TraceContext.Write Metoda

Definicja

Zapisuje informacje śledzenia w dzienniku śledzenia.

Przeciążenia

Write(String)

Zapisuje komunikat śledzenia w dzienniku śledzenia.

Write(String, String)

Zapisuje informacje śledzenia w dzienniku śledzenia, w tym komunikat i wszystkie kategorie zdefiniowane przez użytkownika.

Write(String, String, Exception)

Zapisuje informacje śledzenia w dzienniku śledzenia, w tym wszelkie kategorie zdefiniowane przez użytkownika, komunikaty śledzenia i informacje o błędzie.

Write(String)

Zapisuje komunikat śledzenia w dzienniku śledzenia.

public:
 void Write(System::String ^ message);
public void Write (string message);
member this.Write : string -> unit
Public Sub Write (message As String)

Parametry

message
String

Komunikat śledzenia do zapisu w dzienniku.

Uwagi

Za każdym razem, Write gdy metoda jest wywoływana, TraceContext do kolekcji komunikatów jest dodawany TraceRecords komunikat śledzenia, który jest dostępny podczas obsługi TraceFinished zdarzenia. Komunikat jest dodawany z właściwością ustawioną IsWarning na i jej ErrorInfo właściwość ustawioną na nullfalse .

Zobacz też

Dotyczy

Write(String, String)

Zapisuje informacje śledzenia w dzienniku śledzenia, w tym komunikat i wszystkie kategorie zdefiniowane przez użytkownika.

public:
 void Write(System::String ^ category, System::String ^ message);
public void Write (string category, string message);
member this.Write : string * string -> unit
Public Sub Write (category As String, message As String)

Parametry

category
String

Kategoria śledzenia, która odbiera komunikat.

message
String

Komunikat śledzenia do zapisu w dzienniku.

Przykłady

W poniższym przykładzie kodu pokazano, jak wywołać metodę Write w celu zapisania komunikatu śledzenia błędu w dzienniku śledzenia. W tym przykładzie delegat iteruje komunikaty śledzenia i zapisuje je jako tabelę HTML; można jednak napisać te same informacje do bazy danych lub użytkownika narzędzia profilowania.

<%@ 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.
    Response.Write("<table>");
    foreach(object o in e.TraceRecords)
    {
        r = (TraceContextRecord)o;
        Response.Write(String.Format("<tr><td>{0}</td><td>{1}</td></tr>", r.Message, r.Category));        
    }
    Response.Write("</table>");
}       
</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.
    Response.Write("<table>")
    For Each o In e.TraceRecords
        r = CType(o, TraceContextRecord)
        Response.Write(String.Format("<tr><td>{0}</td><td>{1}</td></tr>", r.Message, r.Category))
    Next
    Response.Write("</table>")

End Sub ' OnTraceFinished
</script>

Uwagi

Za każdym razem, Write gdy metoda jest wywoływana, TraceContext do kolekcji komunikatów jest dodawany TraceRecords komunikat śledzenia, który jest dostępny podczas obsługi TraceFinished zdarzenia. Komunikat jest dodawany z właściwością ustawioną IsWarning na i jej ErrorInfo właściwość ustawioną na nullfalse .

Zobacz też

Dotyczy

Write(String, String, Exception)

Zapisuje informacje śledzenia w dzienniku śledzenia, w tym wszelkie kategorie zdefiniowane przez użytkownika, komunikaty śledzenia i informacje o błędzie.

public:
 void Write(System::String ^ category, System::String ^ message, Exception ^ errorInfo);
public void Write (string category, string message, Exception errorInfo);
member this.Write : string * string * Exception -> unit
Public Sub Write (category As String, message As String, errorInfo As Exception)

Parametry

category
String

Kategoria śledzenia, która odbiera komunikat.

message
String

Komunikat śledzenia do zapisu w dzienniku.

errorInfo
Exception

Element Exception zawierający informacje o błędzie.

Przykłady

W poniższym przykładzie kodu pokazano, jak wywołać metodę Write w celu zapisania komunikatu śledzenia błędu w dzienniku śledzenia. W tym przykładzie różne wyjątki są śledzone jako błędy i ostrzeżenia. Gdy strona zgłasza błąd ArgumentException, zapisuje komunikat ostrzegawczy przy użyciu Warn metody . Gdy strona zgłasza błąd InvalidOperationException, zapisuje komunikat o błędzie przy użyciu Write metody .

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

    try {
        throw new ArgumentException("Trace Test");
    }
    catch (InvalidOperationException ioe) {    
        // You can write an error trace message using the Write method.
        Trace.Write("Exception Handling", "Exception: Page_Load.", ioe);
    }
    catch (ArgumentException ae) {    
        // You can write a warning trace message using the Warn method.
        Trace.Warn("Exception Handling", "Warning: Page_Load.", ae);
    }
}
 
// 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;
        if (r.IsWarning) {
            Response.Write(String.Format("warning message: {0} <BR>", r.Message));
        }
        else {
            Response.Write(String.Format("error 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

    Try 
    Dim ae As New ArgumentException("Trace Test")
        Throw ae
    
    catch ioe As InvalidOperationException
        ' You can write an error trace message using the Write method.
        Trace.Write("Exception Handling", "Exception: Page_Load.", ioe)
    
    Catch ae As ArgumentException
        ' You can write a warning trace message using the Warn method.
        Trace.Warn("Exception Handling", "Warning: Page_Load.", ae)

    End Try

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)
    If r.IsWarning Then
            Response.Write(String.Format("warning message: {0} <BR>", r.Message))
        Else
            Response.Write(String.Format("error message: {0} <BR>", r.Message))
        End If
    Next

End Sub	' OnTraceFinished
</script>

Uwagi

Za każdym razem, Write gdy metoda jest wywoływana, TraceContext do kolekcji komunikatów jest dodawany TraceRecords komunikat śledzenia, który jest dostępny podczas obsługi TraceFinished zdarzenia. Komunikat jest dodawany z IsWarning właściwością ustawioną na false, a ErrorInfo właściwość jest ustawiona na obiekt przekazany przez errorInfo parametr .

Zobacz też

Dotyczy