TraceContextRecord.Message Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the user-defined trace message.

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

public string Message { get; }

Property Value

Type: System.String

A string that represents a message for the trace record.

The message can be any string, and is used to write detailed tracing information. It corresponds to the message parameter that is passed to the TraceContext.Write and TraceContext.Warn methods. The information can be run-time values, messages to orient you and guide you through a program's execution, or other diagnostic data.

The following code example demonstrates how to access the Message property of a TraceContextRecord, and print it 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);

    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>

.NET Framework
Available since 2.0
Return to top
Show: