469,950 Members | 2,334 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

MemoryStream And Trace Listener?

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!
Nov 20 '05 #1
3 2128
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()

"Schorschi" <Sc*******@DSLExtreme.COM> wrote in message
news:21**************************@posting.google.c om...
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!

Nov 20 '05 #2
Nope, tried that as well.
Nov 20 '05 #3
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
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Geopsaros | last post: by
2 posts views Thread by Richard | last post: by
2 posts views Thread by Jason | last post: by
reply views Thread by martin | last post: by
13 posts views Thread by Don | last post: by
reply views Thread by Blake | last post: by
reply views Thread by boldtbanan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.