MemoryStream And Trace Listener?

  • Thread starter Thread starter Schorschi
  • Start date Start date
S

Schorschi

Ok, can someone point out to me why the following code does not work?
In compiles, no errors, but MemoryStream never seems to receive any
data?

Dim theMemory As MemoryStream, _
theListener As TextWriterTraceListener

theMemory = New MemoryStream(4096)
theListener = New TextWriterTraceListener(theMemory)

Trace.AutoFlush = True
Trace.Listeners.Add(theListener)
Trace.WriteLine("Test Memory Stream!")

Dim theReader As StreamReader
Dim theText As String

theReader = New StreamReader(theMemory)

theText = theReader.ReadLine()

Thanks! In Advanced!
 
Perhaps because the Position of the stream is at the end when you try to do
the Readline?

Add theMemory.Position = 0 just before theReader.ReadLine()
 
Talk about answering your own question? LOL. Below is the solution
that works. I am sure others may have different or better ways to
setup a memory based trace-listener, if so maybe they will add to this
message thread?

The idea behind this code was to have a way to capture trace
information for some specialized application-start tasks, that did NOT
incur file I/O, before any of our application configuration settings
are set by .Config file, registry, INI (private profile) or the
commandline, and of course before any of these setting options are
defined by the user. Should the user configure options to disable the
trace information? Then we just toss the queued trace information in
the void, otherwise we redirect it to console, a database, or file
based on the user preference.

Enjoy...

'
'
'

Option Explicit On
Option Strict On

'

Imports System.Windows.Forms.Application
Imports System.IO
Imports System.IO.Path

'

Class ListenerClass
Inherits TraceListener

'

Private theMemory As MemoryStream
Private thePath As String
Private theWriter As StreamWriter

'

Public Sub New()

'

theMemory = New MemoryStream

With theMemory

'

.Position = Nothing

End With

theWriter = New StreamWriter(theMemory)

With theWriter

'

.AutoFlush = True

End With

thePath = Combine(GetDirectoryName(ExecutablePath), _
String.Format("{0} Flush File.LOG",
"MemoryStreamTraceListener"))

File.Delete(thePath)

End Sub

Public Overloads Overrides Sub Write(ByVal theMessage As String)

'

With theWriter

'

.Write(theMessage)

End With

End Sub

Public Overloads Overrides Sub WriteLine(ByVal theMessage As
String)

'

With theWriter

'

.WriteLine(theMessage)

End With

End Sub

Public Overrides Sub Flush()

Dim theFile As FileStream

'

theFile = New FileStream(thePath, FileMode.OpenOrCreate)

With theMemory

'

.WriteTo(theFile)
.SetLength(Nothing)

End With

'

theFile.Close()

End Sub

End Class

Module MainModule

'

Sub Main()

Dim theListener As TraceListener

'

theListener = New ListenerClass
Trace.Listeners.Add(theListener)

Trace.WriteLine("MemoryStreamTraceListener...")
Trace.WriteLine("Egor Its Alive!")
Trace.WriteLine("...Wait, Wait, Wait For It.")

theListener.Flush()

End Sub

End Module
 
Back
Top