Question

I have been writing an app in vb.net to copy the last few lines of an IIS W3C log file every few minusts to A file that will be used for some remote reporting.

  • Everything works when I test on my local PC but when I try it on the live IIS server it tells me the file is locked by another process, I take it IIS has it locked...
  • It does read in some of the sites log files (more than on site on the server) but not others, tells me locked/in use.
  • Why is it that I can always open the file in notepad but not open it in my app?

The Code:

Dim linex = ""
Dim Line = ""

    '### IT ERROS OUT ON THE NEXT LINE ###
Using sr As New StreamReader("C:\inetpub\logs\LogFiles\W3SVC14\u_ex130702.log")

    Do Until sr.EndOfStream
        linex = sr.ReadLine()                
         line = line & linex & vbCrLf           
    Loop

End Using
Was it helpful?

Solution

You need to specify correct sharing options and open mode when creating underlying FileStream. Since there is no constructor of StreamReader that passes all necessary arguments you need to construct FileStream first using FileStream(String, FileMode, FileAccess, FileShare) and than create StreamReader on it using SrteamReader(Stream).

I think following should open IIS log file while it is being written to by IIS (if not - try other combinations of flags)

Using stream As New  New FileStream( _
 "Test#@@#.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
   Using sr As New StreamReader(stream)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top