Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

TextWriterTraceListener.Close Method

Closes the Writer so that it no longer receives tracing or debugging output.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

'Declaration
Public Overrides Sub Close

Calling a Write or WriteLine method after calling Close automatically reopens the stream.

The following example implements a TextWriterTraceListener named myTextListener, which uses a StreamWriter called myOutputWriter to write to a file named TestFile.txt. The example creates the file, stream, and text writer, writes one line of text to the file, and then flushes and closes the stream.


#Const TRACE=True

Imports System
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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft