Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: setting output stream for TextWriterTraceListener


Message #1 by john@j... on Thu, 5 Dec 2002 23:10:56
I am attempting to create a class that inherits from the 
TextWriterTraceListener class.  My thought was to create a class that 
centralizes/standardizes the creation of the log file name.  So the 
approach would be to create a child class of the TextWriterTraceListener 
class and in the Write and WriteLine procedures set a FileHandle property 
on the TextWriterTraceListener to give it somewhere to output the Debug 
statements.  I would make TextWriterTraceListener2 (my new class) part of 
the Listeners collection of my Debug or Trace class.

Here is what I have so far.  My problem is that the only way I can tell 
the TextWriterTraceListener what Stream object to use is in its 
constructor.  Is there any other way?

Thanks,
John

    Public Class TextWriterTraceListener2
        Inherits TextWriterTraceListener

        Private sPath As String
        Private sBaseFilename As String

        Private Function IsLastCharSlash(ByVal sDir As String) As Boolean
            Return InStr("\/", Right(sDir, 1)) = 0
        End Function

#Region "constructors"
        Public Sub New(ByVal sFile As String, ByVal sDir As String)
            If Not IsLastCharSlash(sDir) Then sDir += "/"
            sPath = sDir
            sBaseFilename = sFile
        End Sub
#End Region

        Private ReadOnly Property FileHandle() As System.IO.FileStream
            Get
                Dim sFilePath As String
                sFilePath = sPath + sBaseFilename + String.Format
("_yyyy_MM_dd", Date.Now()) + ".log"
                Dim oFile As New System.IO.FileStream(sFilePath, 
IO.FileMode.Append, IO.FileAccess.Write)
                Return oFile
            End Get
        End Property

        Public Overloads Overrides Sub Write(ByVal sMsg As String)
	    ' I want to do something like myBase.Handle = FileHandle here
            MyBase.Write(sMsg)
        End Sub

        Public Overloads Overrides Sub WriteLine(ByVal sMsg As String)
	    ' I want to do something like myBase.Handle = FileHandle here
            MyBase.WriteLine(sMsg)
        End Sub

        Public Overloads Overrides Sub Write(ByVal sMsg As String, ByVal 
sCategory As String)
	    ' I want to do something like myBase.Handle = FileHandle here
            MyBase.Write(sMsg, sCategory)
        End Sub

        Public Overloads Overrides Sub WriteLine(ByVal sMsg As String, 
ByVal sCategory As String)
	    ' I want to do something like myBase.Handle = FileHandle here
            MyBase.WriteLine(sMsg, sCategory)
        End Sub
    End Class

  Return to Index