Results 1 to 2 of 2

Thread: Log to file 'cannot access' issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Log to file 'cannot access' issue

    I am appending data to a log file but started getting the following error. Is there a better way to do this?

    Error: The process cannot access the file 'c:xx' because it is being used by another process.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.IO.IOException: The process cannot access the file 'C:\..\logs\xxString_Log_20140820.txt' because it is being used by another process.

    Line it says is problem:
    Using fs As FileStream = New FileStream(sFullPath, FileMode.Append, FileAccess.Write)


    Code:
    Public Sub LogIt_Custom(ByVal sLogFileName As String, ByVal sMsg As String)
            If sLogFileName.Trim.Length > 0 Then
                Dim sPath As String = System.AppDomain.CurrentDomain.BaseDirectory & "\logs\"
    
                If Directory.Exists(sPath) = False Then
                    Directory.CreateDirectory(sPath)
                End If
    
                Dim sFullPath As String = sPath & String.Format("{1}_{0}.txt", DateTime.Now.ToString("yyyyMMdd"), sLogFileName)
                Dim sTimeStamp As String = ""
                Using fs As FileStream = New FileStream(sFullPath, FileMode.Append, FileAccess.Write)
                    Using sw As StreamWriter = New StreamWriter(fs)
                        Try
                            sw.WriteLine("{0}--{1}", DateTime.Now.ToString("yyyyMMdd-hhmmss"), sMsg)
                        Catch ex As Exception
    
                        Finally
                            sw.Flush()
                            sw.Close()
                        End Try
                    End Using
                    fs.Dispose()
                    fs.Close()
                End Using
            End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Log to file 'cannot access' issue

    Firstly, I would suggest that you get rid of all of this:
    Code:
                Using fs As FileStream = New FileStream(sFullPath, FileMode.Append, FileAccess.Write)
                    Using sw As StreamWriter = New StreamWriter(fs)
                        Try
                            sw.WriteLine("{0}--{1}", DateTime.Now.ToString("yyyyMMdd-hhmmss"), sMsg)
                        Catch ex As Exception
    
                        Finally
                            sw.Flush()
                            sw.Close()
                        End Try
                    End Using
                    fs.Dispose()
                    fs.Close()
                End Using
    and replace it with this:
    Code:
    Try
        File.AppendAllText(sFullPath,
                           String.Format("{0:yyyyMMdd-HHmmss}--{1}{2}",
                                         DateTime.Now,
                                         sMsg,
                                         Environment.NewLine))
    Catch
    End Try
    Note, amongst other things, the use of 24-hour time. You were not distinguishing in any way between AM and PM. That code will definitely not keep the file open. Mind you, unless you're executing that code on multiple threads simultaneously, it shouldn't keep the file open either. If you still get the same exception after making the suggested change then you must be opening the file elsewhere too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width