TraceContext.Write 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將追蹤資訊寫入追蹤日誌。
多載
| 名稱 | Description |
|---|---|
| Write(String) |
會寫入追蹤訊息到追蹤日誌。 |
| Write(String, String) |
會將追蹤資訊寫入追蹤日誌,包括訊息及任何使用者自訂的分類。 |
| Write(String, String, Exception) |
會將追蹤資訊寫入追蹤日誌,包括任何使用者自訂的分類、追蹤訊息及錯誤資訊。 |
Write(String)
會寫入追蹤訊息到追蹤日誌。
public:
void Write(System::String ^ message);
public void Write(string message);
member this.Write : string -> unit
Public Sub Write (message As String)
參數
- message
- String
寫入日誌的追蹤訊息。
備註
每次呼叫該 Write 方法時, TraceContext 追蹤訊息會被 TraceRecords 加入訊息集合,處理事件時 TraceFinished 可存取。 訊息加入時,屬性IsWarning設為 ,falseErrorInfo屬性設為 null。
另請參閱
適用於
Write(String, String)
會將追蹤資訊寫入追蹤日誌,包括訊息及任何使用者自訂的分類。
public:
void Write(System::String ^ category, System::String ^ message);
public void Write(string category, string message);
member this.Write : string * string -> unit
Public Sub Write (category As String, message As String)
參數
- category
- String
接收訊息的追蹤類別。
- message
- String
寫入日誌的追蹤訊息。
範例
以下程式碼範例示範如何呼叫 Write 該方法,將錯誤追蹤訊息寫入追蹤日誌。 在此範例中,代理會遍歷追蹤訊息並將它們寫成 HTML 表格;然而,你可以將相同的資訊寫入資料庫或分析工具使用者。
<%@ 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>
<%@ 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
' Write a trace message.
Trace.Write("Web Forms Infrastructure Methods", "USERMESSAGE: Page_Load complete.")
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.
Response.Write("<table>")
For Each o In e.TraceRecords
r = CType(o, TraceContextRecord)
Response.Write(String.Format("<tr><td>{0}</td><td>{1}</td></tr>", r.Message, r.Category))
Next
Response.Write("</table>")
End Sub ' OnTraceFinished
</script>
備註
每次呼叫該 Write 方法時, TraceContext 追蹤訊息會被 TraceRecords 加入訊息集合,處理事件時 TraceFinished 可存取。 訊息加入時,屬性IsWarning設為 ,falseErrorInfo屬性設為 null。
另請參閱
適用於
Write(String, String, Exception)
會將追蹤資訊寫入追蹤日誌,包括任何使用者自訂的分類、追蹤訊息及錯誤資訊。
public:
void Write(System::String ^ category, System::String ^ message, Exception ^ errorInfo);
public void Write(string category, string message, Exception errorInfo);
member this.Write : string * string * Exception -> unit
Public Sub Write (category As String, message As String, errorInfo As Exception)
參數
- category
- String
接收訊息的追蹤類別。
- message
- String
寫入日誌的追蹤訊息。
範例
以下程式碼範例示範如何呼叫 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>
備註
每次呼叫該 Write 方法時, TraceContext 追蹤訊息會被 TraceRecords 加入訊息集合,處理事件時 TraceFinished 可存取。 訊息加入時,其 IsWarning 屬性設為 false,屬性 ErrorInfo 則設為參數傳遞 errorInfo 的物件。