I have created a Windows service to open the IIS log, read content, then e-mail this content to consultants that 'The Man' doesn't want to have any access to those servers. On my local machine, everything works great, and the file generated from parsing the IIS log for the day is e-mailed to the recipients.
I do not have access to install my service on the development system. I had the administrator install the service. The service has LocalSystem permissions. When the service runs, I receive this message in the Application log:
LogExtractor: GetIISLog function - C:\WINDOWS\system32\LogFiles\W3SVC1\ex090918.log does not exist.
The administrator has confirmed that the path is correct, and the file does exist. (My user account does not have access to the W3SVC1 directory.) It was my understanding that the System account is the uber-account, and can do pretty much what it wants, so I'm at a loss as to why it cannot read the file. I'm assuming it's a permissions issue, since the files does exist.
Here is the relevant code. I've snipped the logic involved in the section about reading the file. The code works, as it runs successfully on one system, but not another, so I know the path is being generated correctly. Any ideas?
Private Sub GetIISLog(ByVal LastRetrievedDate As DateTime)
'Build the file path to store the IIS event log before sending it off (previous code snipped)
Dim FileDate As String = "ex" & Date.Now.Year.ToString.Substring(2, 2) & Month & Day & ".log"
Dim FileName As String = "C:\WINDOWS\system32\LogFiles\W3SVC1\" & FileDate
'If the file doesn't exist, exit
If Not File.Exists(FileName) Then
LogIssues("LogExtractor: GetIISLog function - " & FileName & " does not exist.", EventLogEntryType.Error)
Exit Sub
End If
Dim s As Stream = Nothing
Try
s = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim strResult As String = String.Empty
Using sr As New StreamReader(s)
Using sw As New StreamWriter(IISLogFile, False)
strResult = sr.ReadLine
While Not strResult Is Nothing
sw.WriteLine(strResult)
'Write the results
strResult = sr.ReadLine
End While
End Using
sr.Close()
End Using
s.Close()
Catch ex As Exception
LogIssues("LogExtractor: Error writing IIS file - " & ex.Message, EventLogEntryType.Error)
Finally
s.Close()
End Try
s = Nothing
End Sub