J
JerryWEC
I'd like to know if there are any known issues with using AppendText()
method. I'm trying to write data to a log file using it and I'm having some
issues. I keep getting an error message from my ProcessException() handler
saying "The process cannot access the file 'C:\Temp\Logging\LogFileX.log'
because it is being used by another process.
Here is my code I was using. Previously I was using a Using statement for
the StreamWriter (sw)...
Private Sub WriteDataToFile(ByVal FileName As String, ByVal DataString As
String)
Dim sw As StreamWriter = File.AppendText(FileName)
Try
If File.Exists(FileName) Then
'Using sw As StreamWriter = File.AppendText(FileName)
sw.WriteLine(DataString)
'sw.Flush() 'Update text to file.
sw.Close() 'Close StreamWriter and underlying file.
'End Using
Else
Throw New FileNotFoundException
End If
Catch ex As Exception
sw.Close() 'Close StreamWriter and underlying file.
Common.Instance.ProcessException(Common.Instance.CurrentMethodName(),
ex)
End Try
End Sub
I am using File.Exist() Shared method and FileInfo objects to see if the
file exist and to check the file size, etc. Could these cause problems
writing to the file? I am writing a lot of messages to the my .log file
using a loop in a test project. I seem to get this error message while
initially writing a message to the file and when I create a new .log file.
When I write to a new .log file it will skip 5 to 8 lines of text between
files. Basically when my current .log file reaches maxium size it will
create a new file and start writing to it; LogFile1.log, LogFile2.log,
LogFile3.log, etc. I have a more elabrate naming conventation with
data/time data but I simplified it here.
Is there a better way to write to a log file besides this File.AppendText()?
I don't believe I was initially getting this error message. But I was not
trapping for it or looking for it either.
I normally will not be writing to the log file this fast but for testing
purposes I am writing in a loop some test messages and it is fast. I tried
to add some Thread.Sleep() between 2 and 100 milliseconds but that only
seems to work a little in places. I then put a Thread.Sleep between each
time I write to log file and still getting the error message.
Help! JerryM
method. I'm trying to write data to a log file using it and I'm having some
issues. I keep getting an error message from my ProcessException() handler
saying "The process cannot access the file 'C:\Temp\Logging\LogFileX.log'
because it is being used by another process.
Here is my code I was using. Previously I was using a Using statement for
the StreamWriter (sw)...
Private Sub WriteDataToFile(ByVal FileName As String, ByVal DataString As
String)
Dim sw As StreamWriter = File.AppendText(FileName)
Try
If File.Exists(FileName) Then
'Using sw As StreamWriter = File.AppendText(FileName)
sw.WriteLine(DataString)
'sw.Flush() 'Update text to file.
sw.Close() 'Close StreamWriter and underlying file.
'End Using
Else
Throw New FileNotFoundException
End If
Catch ex As Exception
sw.Close() 'Close StreamWriter and underlying file.
Common.Instance.ProcessException(Common.Instance.CurrentMethodName(),
ex)
End Try
End Sub
I am using File.Exist() Shared method and FileInfo objects to see if the
file exist and to check the file size, etc. Could these cause problems
writing to the file? I am writing a lot of messages to the my .log file
using a loop in a test project. I seem to get this error message while
initially writing a message to the file and when I create a new .log file.
When I write to a new .log file it will skip 5 to 8 lines of text between
files. Basically when my current .log file reaches maxium size it will
create a new file and start writing to it; LogFile1.log, LogFile2.log,
LogFile3.log, etc. I have a more elabrate naming conventation with
data/time data but I simplified it here.
Is there a better way to write to a log file besides this File.AppendText()?
I don't believe I was initially getting this error message. But I was not
trapping for it or looking for it either.
I normally will not be writing to the log file this fast but for testing
purposes I am writing in a loop some test messages and it is fast. I tried
to add some Thread.Sleep() between 2 and 100 milliseconds but that only
seems to work a little in places. I then put a Thread.Sleep between each
time I write to log file and still getting the error message.
Help! JerryM