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 ()
属性

示例

以下示例创建一个名为 TextWriterTraceListenermyTextListenermyTextListenerStreamWriter使用调用 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,其中某些字符只能在编码器收到相邻字符后进行编码。

适用于

另请参阅