TraceContext.Write 方法

定义

将跟踪信息写入跟踪日志。

重载

Write(String)

将跟踪消息写入跟踪日志。

Write(String, String)

将跟踪信息写入跟踪日志,包括消息和任何用户定义的类别。

Write(String, String, Exception)

将跟踪信息写入跟踪日志,包括用户定义的所有类别、跟踪消息和错误消息。

Write(String)

将跟踪消息写入跟踪日志。

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

参数

message
String

要写入日志的跟踪消息。

注解

每次 Write 调用 方法时, TraceContext 都会将跟踪消息添加到消息集合, TraceRecords 可以在处理事件时访问该 TraceFinished 集合。 添加消息时,其 IsWarning 属性设置为 falseErrorInfo 属性设置为 null

另请参阅

适用于

Write(String, String)

将跟踪信息写入跟踪日志,包括消息和任何用户定义的类别。

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

接收消息的跟踪类别。

message
String

要写入日志的跟踪消息。

示例

下面的代码示例演示如何调用 Write 方法以将错误跟踪消息写入跟踪日志。 在此示例中,委托循环访问跟踪消息并将其写入 HTML 表;但是,可以将相同的信息写入数据库或分析工具使用者。

<%@ 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 集合。 添加消息时,其 IsWarning 属性设置为 falseErrorInfo 属性设置为 null

另请参阅

适用于

Write(String, String, Exception)

将跟踪信息写入跟踪日志,包括用户定义的所有类别、跟踪消息和错误消息。

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

接收消息的跟踪类别。

message
String

要写入日志的跟踪消息。

errorInfo
Exception

包含有关错误消息的 Exception

示例

下面的代码示例演示如何调用 Write 方法以将错误跟踪消息写入跟踪日志。 在此示例中,不同的异常作为错误和警告进行跟踪。 当页面引发 时 ArgumentException,它会使用 Warn 方法写入警告消息。 当页面引发 时 InvalidOperationException,它会使用 Write 方法写入错误消息。

<%@ 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 集合。 添加消息时,其 IsWarning 属性设置为 false,属性 ErrorInfo 设置为 参数 errorInfo 传递的对象。

另请参阅

适用于