次の方法で共有


TextWriterTraceListener.Flush メソッド

定義

Writer 用の出力バッファーをフラッシュします。

public:
 override void Flush();
public override void Flush ();
override this.Flush : unit -> unit
Public Overrides Sub Flush ()

次の例では、 という名前myTextListenerの をTextWriterTraceListener実装します。これは、 という名前TestFile.txtStreamWriterファイルに書き込むための 呼び出しmyOutputWriterを使用します。 この例では、ファイル、ストリーム、およびテキスト ライターを作成し、1 行のテキストをファイルに書き込み、ストリームをフラッシュして閉じます。

#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

適用対象

こちらもご覧ください