语言

TraceContext.Warn 方法

定义

将跟踪信息写入跟踪日志。 与日志 Write(String)不同,所有警告都以红色文本的形式显示在日志中。

重载

名称 说明
Warn(String)

将跟踪消息写入跟踪日志。 所有警告都以红色文本的形式显示在日志中。

Warn(String, String)

将跟踪信息写入跟踪日志,包括任何用户定义的类别和跟踪消息。 所有警告都以红色文本的形式显示在日志中。

Warn(String, String, Exception)

将跟踪信息写入跟踪日志,包括任何用户定义的类别、跟踪消息和错误信息。 所有警告都以红色文本的形式显示在日志中。

Warn(String)

将跟踪消息写入跟踪日志。 所有警告都以红色文本的形式显示在日志中。

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

参数

message
String

要写入日志的跟踪消息。

注解

每次调用该方法时Warn,都会向消息集合添加TraceContext跟踪TraceRecords消息,该集合在处理TraceFinished事件时可访问。 将添加消息的属性 IsWarning 设置为 true ,并且其 ErrorInfo 属性设置为 null.

另请参阅

适用于

Warn(String, String)

将跟踪信息写入跟踪日志,包括任何用户定义的类别和跟踪消息。 所有警告都以红色文本的形式显示在日志中。

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

参数

category
String

接收消息的跟踪类别。

message
String

要写入日志的跟踪消息。

注解

每次调用该方法时Warn,都会向消息集合添加TraceContext跟踪TraceRecords消息,该集合在处理TraceFinished事件时可访问。 将添加消息的属性 IsWarning 设置为 true ,并且其 ErrorInfo 属性设置为 null.

另请参阅

适用于

Warn(String, String, Exception)

将跟踪信息写入跟踪日志,包括任何用户定义的类别、跟踪消息和错误信息。 所有警告都以红色文本的形式显示在日志中。

public:
 void Warn(System::String ^ category, System::String ^ message, Exception ^ errorInfo);
public void Warn(string category, string message, Exception errorInfo);
member this.Warn : string * string * Exception -> unit
Public Sub Warn (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>

注解

每次调用该方法时Warn,都会向消息集合添加TraceContext跟踪TraceRecords消息,该集合在处理TraceFinished事件时可访问。 将添加消息,其 IsWarning 属性设置为 true该消息,并将 ErrorInfo 该属性设置为参数 errorInfo 传递的对象。

另请参阅

适用于