TextWriterTraceListener.Close 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
關閉 Writer 使其不再接收追蹤或偵錯的輸出。
public:
override void Close();
public override void Close ();
override this.Close : unit -> unit
Public Overrides Sub Close ()
範例
下列範例會實作 TextWriterTraceListener 名為 myTextListener
的 ,其會使用 StreamWriter 呼叫 myOutputWriter
寫入名為 的 TestFile.txt
檔案。 此範例會建立檔案、數據流和文字寫入器、將一行文字寫入檔案,然後排清並關閉數據流。
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;
void main()
{
#if defined(TRACE)
TextWriterTraceListener^ myTextListener = nullptr;
// Create a file for output named TestFile.txt.
String^ myFileName = "TestFile.txt";
StreamWriter^ myOutputWriter = gcnew StreamWriter( myFileName,true );
// Add a TextWriterTraceListener for the file.
if ( myOutputWriter )
{
myTextListener = gcnew TextWriterTraceListener( myOutputWriter );
Trace::Listeners->Add( myTextListener );
}
// Write trace output to all trace listeners.
Trace::WriteLine(
String::Concat( DateTime::Now.ToString(), " - Trace output" ) );
if ( myTextListener )
{
// Remove and close the file writer/trace listener.
myTextListener->Flush();
Trace::Listeners->Remove( myTextListener );
myTextListener->Close();
}
#endif
}
#define TRACE
using System;
using System.IO;
using System.Diagnostics;
public class TextWriterTraceListenerSample
{
public static void Main()
{
TextWriterTraceListener myTextListener = null;
// Create a file for output named TestFile.txt.
string myFileName = "TestFile.txt";
StreamWriter myOutputWriter = new StreamWriter(myFileName, true);
// Add a TextWriterTraceListener for the file.
myTextListener = new TextWriterTraceListener(myOutputWriter);
Trace.Listeners.Add(myTextListener);
// Write trace output to all trace listeners.
Trace.WriteLine(DateTime.Now.ToString() + " - Trace output");
// Remove and close the file writer/trace listener.
myTextListener.Flush();
Trace.Listeners.Remove(myTextListener);
myTextListener.Close();
}
}
#Const TRACE=True
Imports System.IO
Imports System.Diagnostics
Public Class TextWriterTraceListenerSample
Public Shared Sub Main()
Dim myTextListener As TextWriterTraceListener = Nothing
' Create a file for output named TestFile.txt.
Dim myFileName As String = "TestFile.txt"
Dim myOutputWriter As New StreamWriter(myFileName, True)
' Add a TextWriterTraceListener for the file.
myTextListener = New TextWriterTraceListener(myOutputWriter)
Trace.Listeners.Add(myTextListener)
' Write trace output to all trace listeners.
Trace.WriteLine(DateTime.Now.ToString() + " - Trace output")
' Remove and close the file writer/trace listener.
myTextListener.Flush()
Trace.Listeners.Remove(myTextListener)
myTextListener.Close()
End Sub
End Class
備註
Write在呼叫 Close 後呼叫 或 WriteLine 方法會自動重新開啟數據流。