Class Writer
Inherits TextWriter 'This is the original class for the console's output
Private mEncoding As Encoding 'Stores the encoding, passed from the original Console Output Stream
Private mFileStream As FileStream 'Stores the FileStream to write to (the log file)
Private mConsoleStream As Stream 'Stores the Stream to write to (so the messages still appear on the console window), this is also passed from the original Console Output Stream
Public Sub New(ByVal encoding As Encoding, ByVal fStream As FileStream, ByVal consoleStream As Stream)
'Constructor: takes the encoding and console stream from the original console output and the filestream to write to
mEncoding = encoding
mFileStream = fStream
mConsoleStream = consoleStream
End Sub
Public Overrides ReadOnly Property Encoding As Encoding
'Required, as Encoding is declared as MustOverride in the TextWriter class
Get
Return mEncoding 'Returns the encoding we took from the original console stream
End Get
End Property
Public Overrides Sub WriteLine(ByVal value As String)
'Here we are overriding the WriteLine function. Basically, we are just adding a new line to the end of the message and calling the Write function
value &= vbNewLine
Write(value)
End Sub
Public Overrides Sub Write(ByVal value As String)
'Here is where we actually write to both streams
Dim bytes As Byte() = Encoding.GetBytes(value) 'First we need to get the bytes to write, we do this by calling the GetBytes function from the encoding and passing the string
mFileStream.Write(bytes, 0, bytes.Length) 'Write to the log file
mConsoleStream.Write(bytes, 0, bytes.Length) 'Write to the console window
End Sub
End Class