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

要寫入記錄檔的追蹤訊息。

備註

每次呼叫 方法時 WarnTraceContext 都會將追蹤訊息新增至 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

要寫入記錄檔的追蹤訊息。

備註

每次呼叫 方法時 WarnTraceContext 都會將追蹤訊息新增至 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>

備註

每次呼叫 方法時 WarnTraceContext 都會將追蹤訊息新增至 TraceRecords 訊息集合,當您處理事件時,即可存取該 TraceFinished 集合。 訊息會將其 IsWarning 屬性設定 true 為 ,而 ErrorInfo 屬性會設定為 參數所 errorInfo 傳遞的物件。

另請參閱

適用於