共用方式為


Trace.Close 方法

定義

清除輸出緩衝區,然後關閉 Listeners

public:
 static void Close();
[System.Diagnostics.Conditional("TRACE")]
public static void Close ();
[<System.Diagnostics.Conditional("TRACE")>]
static member Close : unit -> unit
Public Shared Sub Close ()
屬性

範例

下列範例會 TextWriterTraceListener 建立名為 myTextListener的 。 myTextListenerStreamWriter會使用呼叫 myOutputWriter 寫入名為 的TestFile.txt檔案。 此範例會建立檔案、數據流和文字寫入器、將一行文字寫入檔案,然後排清並關閉輸出。

// Specify /DTRACE when compiling.

#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;

void main()
{
     #if defined(TRACE)
    // Create a file for output named TestFile.txt.
    FileStream^ myFileStream = 
        gcnew FileStream( "TestFile.txt", FileMode::Append );
   
    // Create a new text writer using the output stream 
    // and add it to the trace listeners.
    TextWriterTraceListener^ myTextListener = 
        gcnew TextWriterTraceListener( myFileStream );
    Trace::Listeners->Add( myTextListener );
   
    // Write output to the file.
    Trace::WriteLine( "Test output" );
   
    // Flush and close the output stream.
    Trace::Flush();
    Trace::Close();
    #endif
}
// Specify /d:TRACE when compiling.

using System;
using System.IO;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        // Create a file for output named TestFile.txt.
        using (FileStream myFileStream =
            new FileStream("TestFile.txt", FileMode.Append))
        {
            // Create a new text writer using the output stream
            // and add it to the trace listeners.
            TextWriterTraceListener myTextListener =
                new TextWriterTraceListener(myFileStream);
            Trace.Listeners.Add(myTextListener);

            // Write output to the file.
            Trace.WriteLine("Test output");

            // Flush and close the output stream.
            Trace.Flush();
            Trace.Close();
        }
    }
}
' Specify /d:TRACE=True when compiling.

Imports System.IO
Imports System.Diagnostics

Class Test
    
    Shared Sub Main()
    
        ' Create a file for output named TestFile.txt.
        Using myFileStream As New FileStream("TestFile.txt", FileMode.Append)
        
            ' Create a new text writer using the output stream
            ' and add it to the trace listeners. 
            Dim myTextListener As New TextWriterTraceListener(myFileStream)
            Trace.Listeners.Add(myTextListener)
            
            ' Write output to the file.
            Trace.WriteLine("Test output")
            
            ' Flush and close the output stream.
            Trace.Flush()
            Trace.Close()
        
        End Using
        
    End Sub

End Class

備註

當輸出進入檔案時,請使用這個方法,例如 。TextWriterTraceListener

除非您明確呼叫 FlushClose,否則清除數據流不會排清其基礎編碼器。 設定 AutoFlushtrue 表示數據會從緩衝區排清到數據流,但不會清除編碼器狀態。 這可讓編碼器保持其狀態 (部分字元) ,以便正確編碼下一個字元區塊。 此案例會影響UTF8和UTF7,其中某些字元只能在編碼器收到相鄰字元或字元之後進行編碼。

適用於

另請參閱