Export (0) Print
Expand All

TraceContext.Warn Method (String, String, Exception)

Writes trace information to the trace log, including any user-defined categories, trace messages, and error information. All warnings appear in the log as red text.

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

public void Warn(
	string category,
	string message,
	Exception errorInfo
)

Parameters

category
Type: System.String
The trace category that receives the message.
message
Type: System.String
The trace message to write to the log.
errorInfo
Type: System.Exception
An Exception that contains information about the error.

Every time the Warn 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 true, and the ErrorInfo property is set to the object passed by the errorInfo parameter.

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. When the page throws an ArgumentException, it writes a warning message using the Warn method. 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>


.NET Framework

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft