TraceContext.Write TraceContext.Write TraceContext.Write TraceContext.Write Method

定義

將追蹤資訊寫入檢測檔。Writes trace information to the trace log.

多載

Write(String) Write(String) Write(String) Write(String)

將追蹤資訊寫入檢測檔。Writes a trace message to the trace log.

Write(String, String) Write(String, String) Write(String, String) Write(String, String)

將追蹤資訊寫入檢測檔,包括訊息和任何使用者定義的分類。Writes trace information to the trace log, including a message and any user-defined categories.

Write(String, String, Exception) Write(String, String, Exception) Write(String, String, Exception) Write(String, String, Exception)

將追蹤資訊寫入追蹤記錄檔,包括任何使用者定義的分類、追蹤訊息和錯誤資訊。Writes trace information to the trace log, including any user-defined categories, trace messages, and error information.

Write(String) Write(String) Write(String) Write(String)

將追蹤資訊寫入檢測檔。Writes a trace message to the trace log.

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

參數

message
String String String String

要寫入記錄檔的追蹤訊息。The trace message to write to the log.

備註

每次Write呼叫方法時,TraceContext追蹤訊息加入至TraceRecords訊息集合,也就是可存取,當您處理TraceFinished事件。Every time the Write method is called, a TraceContext trace message is added to the TraceRecords messages collection, which is accessible when you handle the TraceFinished event. 隨即加入訊息,且其IsWarning屬性設定為false並將其ErrorInfo屬性設定為nullThe message is added with its IsWarning property set to false and its ErrorInfo property set to null.

另請參閱

Write(String, String) Write(String, String) Write(String, String) Write(String, String)

將追蹤資訊寫入檢測檔,包括訊息和任何使用者定義的分類。Writes trace information to the trace log, including a message and any user-defined categories.

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)

參數

category
String String String String

接收訊息的追蹤分類。The trace category that receives the message.

message
String String String String

要寫入記錄檔的追蹤訊息。The trace message to write to the log.

範例

下列程式碼範例示範如何呼叫Write方法,將錯誤追蹤訊息寫入追蹤記錄檔。The following code example demonstrates how to call the Write method to write an error trace message to the trace log. 在此範例中,委派會逐一查看的追蹤訊息並將其寫入作為一個 HTML 資料表;不過,您可以撰寫的相同資訊的資料庫或程式碼剖析工具取用者。In this example, the delegate iterates through the trace messages and writes them as an HTML table; however, you can write the same information to a database or a profiling tool consumer.

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

備註

每次Write呼叫方法時,TraceContext追蹤訊息加入至TraceRecords訊息集合,也就是可存取,當您處理TraceFinished事件。Every time the Write method is called, a TraceContext trace message is added to the TraceRecords messages collection, which is accessible when you handle the TraceFinished event. 隨即加入訊息,且其IsWarning屬性設定為false並將其ErrorInfo屬性設定為nullThe message is added with its IsWarning property set to false and its ErrorInfo property set to null.

另請參閱

Write(String, String, Exception) Write(String, String, Exception) Write(String, String, Exception) Write(String, String, Exception)

將追蹤資訊寫入追蹤記錄檔,包括任何使用者定義的分類、追蹤訊息和錯誤資訊。Writes trace information to the trace log, including any user-defined categories, trace messages, and error information.

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)

參數

category
String String String String

接收訊息的追蹤分類。The trace category that receives the message.

message
String String String String

要寫入記錄檔的追蹤訊息。The trace message to write to the log.

errorInfo
Exception Exception Exception Exception

Exception,包含關於錯誤的資訊。An Exception that contains information about the error.

範例

下列程式碼範例示範如何呼叫Write方法,將錯誤追蹤訊息寫入追蹤記錄檔。The following code example demonstrates how to call the Write method to write an error trace message to the trace log. 在此範例中,不同的例外狀況會視為錯誤和警告追蹤。In this example, different exceptions are traced as errors and warnings. 頁面會擲回ArgumentException,它會寫入警告訊息,使用Warn方法。When the page throws an ArgumentException, it writes a warning message using the Warn method. 頁面會擲回InvalidOperationException,它會寫入錯誤訊息使用Write方法。When the page throws an InvalidOperationException, it writes an error message using the Write method.

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

備註

每次Write呼叫方法時,TraceContext追蹤訊息加入至TraceRecords訊息集合,也就是可存取,當您處理TraceFinished事件。Every time the Write method is called, a TraceContext trace message is added to the TraceRecords messages collection, which is accessible when you handle the TraceFinished event. 加上訊息及其IsWarning屬性設定為false,而ErrorInfo屬性設定為所傳遞的物件errorInfo參數。The message is added with its IsWarning property set to false, and the ErrorInfo property is set to the object passed by the errorInfo parameter.

另請參閱

適用於