Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

TraceContext.Write Method (String, String)

Writes trace information to the trace log, including a message and any user-defined categories.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public void Write(
	string category,
	string message
)

Parameters

category
Type: System.String

The trace category that receives the message.

message
Type: System.String

The trace message to write to the log.

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. The message is added with its IsWarning property set to false and its ErrorInfo property set to null.

The following code example demonstrates how to call the Write method to write an error trace message to the trace log. 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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft