locked
Error while writing into text file RRS feed

  • Question

  • User156682895 posted

    Hi All

    Application log file have been created to monitor the process and following code has been used to write the log information into text file. For the first time there is no error encountered when the application runs for hours, may be 3 hours i am getting following error "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll" 

     

         Public Sub WriteToEventLog(ByVal ErrMSG As String, Optional ByVal NewFile As Boolean = False, Optional ByVal NewFile2 As Boolean = False)
            ErrMSG = Format(Now, "dd-MM-yyyy hh:mm:ss") + "===>" + ErrMSG + vbNewLine

            'Dim sFileName As String = Application.StartupPath & "\DP_Error.log"
            Dim sFileName As String = ""
            sFileName = Application.StartupPath & "\" & Format(Now, "yyMMdd") & ".log"
            'If NewFile2 = True Then sFileName = Application.StartupPath & "\DP_CheckExisting.log"
            If IO.File.Exists(sFileName) = False Then
                Dim NewLogFile As New System.IO.FileStream(sFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)
                NewLogFile.Close()
            End If

            Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.Append, FileAccess.Write, FileShare.None)
            myFileStream.Lock(0, 100)
            Dim myWriter As New System.IO.StreamWriter(myFileStream)
            myWriter.Write(ErrMSG)
            myWriter.Flush()
            myWriter.Close()
            myFileStream.Close()
            myFileStream.Unlock(0, 100)


        End Sub

     

    Can anyone help me please ?

    Sunday, January 30, 2011 3:37 AM

Answers

  • User-1412735316 posted

    I can search for you. but everytime you entering this method you are closing the stream so the time can't be a factor for this method. please set a breakpoint at the begining of this method and try to debug each time it enters this method. problem may be at elsewhere. if it's possible then you may share the code from which it's called (though i m not a vb.net expert i can try). kmnur@hotmail.com is my email address.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 30, 2011 1:45 PM

All replies

  • User-1412735316 posted
    Haven't seen ur code since i m on the way and using cell. Plz check the file whether the log has written or not. Then u can debug from last logd process to get actual error. The only error could b - ur log file is open while trying to write into it. Bt ur error message is for other reason than log file. Debug could b the best option.
    Sunday, January 30, 2011 5:03 AM
  • User-1412735316 posted

    why you are trying to unlocking a filestream after closing it?

    Sunday, January 30, 2011 6:28 AM
  • User156682895 posted

    Thanks for reply..

    I havent open the file while writing the log info. After reading couple of blogs i understand the same function called recursively and causing the error so i just mentioned exclusive lock. I am getting the error in the following code 

         Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.Append, FileAccess.Write, FileShare.None)
            myFileStream.Lock(0, 100)
            Dim myWriter As New System.IO.StreamWriter(myFileStream)
            myWriter.Write(ErrMSG)
            myWriter.Flush()
            myWriter.Close()
            myFileStream.Close()
            myFileStream.Unlock(0, 100)

     

     

    Sunday, January 30, 2011 7:42 AM
  • User-1412735316 posted

    you can't use unlock() method when already the file is closed. please change your code as below


     Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.Append, FileAccess.Write, FileShare.None)
            myFileStream.Lock(0, 100)
            Dim myWriter As New System.IO.StreamWriter(myFileStream)
            myWriter.Write(ErrMSG)
            myWriter.Flush()
            myWriter.Close()
            myFileStream.Close()
            myFileStream.Unlock(0, 100)


    Sunday, January 30, 2011 8:41 AM
  • User-1412735316 posted

    If you are still encountering the same error then please share the code block from where you are calling the WriteToEventLog() method.

    Though I am not a vb.net expert i have tested your code block and it's working fine. there may be any problem with the wrong recursion call.

    Sunday, January 30, 2011 8:49 AM
  • User156682895 posted

    Hi Shuvo, 

    Thanks for your reply, You mean to say that file stream must unlock and close it. Can you elaborate it ?

            myFileStream.Unlock(0, 100)

            myFileStream.Close()

    Sunday, January 30, 2011 9:01 AM
  • User156682895 posted

    Hi Shuvo,

    Can i give exclusive lock to WriteToEventLog() method which i mentioned earlier. Do you know any other method for exclusive lock for file writing method ?

     


    Sunday, January 30, 2011 9:04 AM
  • User-1412735316 posted

    I think your locking mechanism is ok. The problem is may be in the code block from where you are calling the method. The type of error message you are getting is normally caused by deep recursion or something like that. I have tested your code as following.

    (you may have a look at following link:     http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx)


    Partial Class Default3
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Dim myFileStream As New System.IO.FileStream("testlog.txt", IO.FileMode.Append, System.IO.FileAccess.Write,IO.FileShare.None)
    'myFileStream.Lock(0, 100)
    'Dim myWriter As New System.IO.StreamWriter(myFileStream)
    'myWriter.Write("123456")
    'myWriter.Flush()
    'myFileStream.Unlock(0, 100)
    'myWriter.Close()
    'myFileStream.Close()
    Dim i As Integer
    For i = 1 To 100
    WriteToEventLog(i)
    Next


    End Sub
    Public Sub WriteToEventLog(ByVal ErrMSG As String, Optional ByVal NewFile As Boolean = False, Optional ByVal NewFile2 As Boolean = False)
    ErrMSG = Format(Now, "dd-MM-yyyy hh:mm:ss") + "===>" + ErrMSG + vbNewLine

    'Dim sFileName As String = Application.StartupPath & "\DP_Error.log"
    Dim sFileName As String = ""
    sFileName = "c:\\testlog.txt"
    'If NewFile2 = True Then sFileName = Application.StartupPath & "\DP_CheckExisting.log"
    If IO.File.Exists(sFileName) = False Then
    Dim NewLogFile As New System.IO.FileStream(sFileName, IO.FileMode.CreateNew, IO.FileAccess.ReadWrite, IO.FileShare.None)
    NewLogFile.Close()
    End If

    Dim myFileStream As New System.IO.FileStream(sFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.None)
    myFileStream.Lock(0, 100)
    Dim myWriter As New System.IO.StreamWriter(myFileStream)
    myWriter.Write(ErrMSG)
    myWriter.Flush()
    myFileStream.Unlock(0, 100)
    myWriter.Close()
    myFileStream.Close()


    End Sub


    End Class




    Sunday, January 30, 2011 10:06 AM
  • User156682895 posted

    Thanks for your effort 

    I have tested the application the way you said it. Dim myWriter As New System.IO.StreamWriter(myFileStream) ->in this code i am getting the error. Application encounter this error only after running couple of hours.  Do you know any other method for exclusive lock for file writing method ?

    Sunday, January 30, 2011 1:33 PM
  • User-1412735316 posted

    I can search for you. but everytime you entering this method you are closing the stream so the time can't be a factor for this method. please set a breakpoint at the begining of this method and try to debug each time it enters this method. problem may be at elsewhere. if it's possible then you may share the code from which it's called (though i m not a vb.net expert i can try). kmnur@hotmail.com is my email address.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 30, 2011 1:45 PM