Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

problem writing to logfile

TCaptainTCaptain Posts: 20Member
I have a peculiar problem and its VERY likely because I'm not getting the nuances of system.io.streamwriter. I've been digging in the help files and googling but I've come up blank.

I've got a small (very simple) log file class that opens, writes to a file and closes in the dispose method (that in itself may be a problem...I don't know).

It works, gives me no errors, but ALL my logfiles just cut off...like the last dozen lines or so never get written out. In debug mode, I put breakpoints and it gives no errors and seems ok...but still nothing in the output file, which is weird because 75% of it does get written out without a problem and its very simple code...can anyone help?

[code]
Public Class log

Private oFile As System.IO.File
Private oWrite As System.IO.StreamWriter

Private strFilename As String
Private strPath As String

ReadOnly Property open() As Boolean
Get
Return boolFileStatus
End Get

End Property

ReadOnly Property Name() As String
Get
Return strFilename
End Get

End Property

ReadOnly Property Path() As String
Get
Return strPath
End Get
End Property


Sub New()
'create logfile in current path with current name based on date and time

Dim strExport As String
Dim strNow As String

strNow = Now().ToString

strNow = strNow.Replace("/", "")
strNow = strNow.Replace(":", "")

strFilename = strNow & "logfile.txt"
strPath = Application.StartupPath.ToString

Try

oWrite = oFile.CreateText(strPath & strFilename)
oWrite.WriteLine("Starting LogFile - " & strNow)

Catch ex As Exception

Throw New Exception("Log file could not be created - error")

End Try

End Sub

Sub New(ByVal strPathGiven As String)
'create logfile in given path with name based on date and time

Dim strExport As String
Dim strNow As String

strNow = Now().ToString

strNow = strNow.Replace("/", "")
strNow = strNow.Replace(":", "")

strFilename = strNow & "logfile.txt"
strPath = strPathGiven

Try

oWrite = oFile.CreateText(strPath & strFilename)
oWrite.WriteLine("Starting LogFile - " & strNow)

Catch ex As Exception

Throw New Exception("Log file could not be created - error")

End Try

End Sub

Sub New(ByVal strPathGiven As String, ByVal strName As String)
'create logfile in given path with given name with date and time added
Dim strExport As String

strFilename = strName
strPath = strPathGiven

Try

oWrite = oFile.CreateText(strPath & strFilename)
oWrite.WriteLine("Starting LogFile - " & Now.ToString)

Catch ex As Exception

Throw New Exception("Log file could not be created - error")

End Try

End Sub

Sub Dispose()

oWrite.Close()

End Sub

[/code]

As you can see its very rudimentary and I was hoping to flesh it out as I go.

Comments

  • rlcrlc Posts: 448Member
    Do you have a write funciton? Didn't see one there. Does your first line show "starting log"? Most likely the problem could be casued by write calls attempting to access the file mulitple times simutanuously, and or mulitple instances of the class trying to open the file.

    I would in general, make this class a singleton with a private constructor. And add a lock/muex of some sort to ensure you are not trying to write to the file at same time.

    ~rlc
  • TCaptainTCaptain Posts: 20Member
    Sorry about that...cut and paste screw up I missed some of the last part of my code:

    [code]
    Sub writelogline(ByVal strLine As String)

    Try

    oWrite.WriteLine(strLine)

    Catch ex As Exception

    Throw New Exception("There was a problem writing to your application logfile")


    End Try

    End Sub

    End Class

    [/code]


    Now, the file gets created and about 20-30 lines DO get written, but in the normal course of my app's operation, there should be an additional 10-20 lines still with a final signoff...and I don't get those, it seems like halfway through it just stops writing to the file. Stepping through it and breakpoints only show normal ops...no errors.


    : I would in general, make this class a singleton with a private constructor. And add a lock/muex of some sort to ensure you are not trying to write to the file at same time.


    I'm not exactly sure what you mean by that (I'm a bit new).
    What I was thinking was that instead of opening the file at the start of my app and closing on the app close, I'd be better off openning and closing each call to .writelogline...I wasn't sure which way to go.
  • rlcrlc Posts: 448Member
    [b][red]This message was edited by rlc at 2005-10-4 6:11:38[/red][/b][hr]
    OK quick description.... A Singleton is a class that encapsulates its own instance and never has more then one instance running in the code. The way you do this is making the Constructor (Sub New) private so that it may only be accessed by the class itself. The class has a public function you make 'GetInstance' that checks if its local instance has been created. If it has it passes that back, else it creates it and passes it back. Now you can put this class in a shared resouce (like a dll) and shared it between different components of your code which will log to the same file through the same instance. If you make this logging class right and put it in a DLL you will be able to reuse it over and over in future projects.

    [code]
    Public NotInheritable Class Logging

    Private Shared m_Logging As Logging

    Public Shared Function GetInstance(ByVal logname As String) As Logging
    If m_Logging Is Nothing Then
    m_Logging = New Logging(logname)
    End If

    Return m_Logging
    End Function

    Private Sub New(ByVal LogName As String)
    MyBase.new()
    'or do your file stuff here
    'instead of holding it open you might open to write to it then close.
    m_FileName = LogName
    End Sub[/code]

    Mutex is a lock, it might not be the best losk for this, I think it might be more resource intensive then some of the other locks. I would try the readerwriter lock like so....
    [code]
    'logging class member
    private m_lock As New System.Threading.ReaderWriterLock

    'in write event code
    Try
    'you might set a timeout instead of infinite
    m_lock.AcquireWriterLock(Timeout.Infinite)

    'write to log

    Catch ex As Exception
    'handle error
    Finally
    If m_lock.IsWriterLockHeld Then
    m_lock.ReleaseWriterLock()
    End If
    End Try
    [/code]

    ~rlc

    EDIT: PS do not!!!!!! Call your Writeevent in your own code if there is an error in the class because it will cause an infinite loop and may act much like what you said nothing else is logging instead log errors in the logging class to the EventLog.


  • TCaptainTCaptain Posts: 20Member
    Thank you rlc!

    I've managed to make it work by opening and closing each write.
    Performance isn't an issue (we're talking max, a few hundred lines here) so I'm sure it won't be a problem.

    however your post was a learning experience and now I'm looking at my code again and using what you showed will make the class better, so I'll try and play with that :)
Sign In or Register to comment.