2009-09-18 64 views
2

我已經創建了一個Windows服務來打開IIS日誌,閱讀內容,然後通過電子郵件將此內容發送給「The Man」不願意訪問這些內容的顧問服務器。在我的本地機器上,一切正常,並且通過解析當天的IIS日誌生成的文件通過電子郵件發送給收件人。Windows服務問題 - LocalSystem帳戶無法讀取文件

我無權在開發系統上安裝我的服務。我讓管理員安裝服務。該服務具有LocalSystem權限。當服務運行時,我在應用程序日誌中收到此消息:

LogExtractor:GetIISLog函數 - C:\ WINDOWS \ system32 \ LogFiles \ W3SVC1 \ ex090918.log不存在。

管理員已確認路徑是否正確,並且文件確實存在。 (我的用戶帳戶無權訪問W3SVC1目錄。)我的理解是,系統帳戶是超級帳戶,並且可以做到它想要的東西,所以我不知道它爲什麼無法閱讀文件。我假設這是一個權限問題,因爲這些文件確實存在。

這裏是相關的代碼。我已經縮減了關於閱讀文件的章節中涉及的邏輯。代碼工作,因爲它在一個系統上成功運行,但不是另一個,所以我知道路徑正在正確生成。有任何想法嗎?

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 
+0

你確定它是以LocalSystem而不是LocalService運行嗎? – ongle 2009-09-18 13:29:37

+0

我再次檢查並且.NET安裝程序正在使用LocalSystem帳戶。 – Scott 2009-09-18 19:16:19

回答

0

我想出了這個問題。 IIS已配置爲登錄到數據庫,因此我正在查找的文件不存在。不幸的是,我既沒有訪問W3SVC1文件夾也沒有訪問IIS,所以我從來不知道這一點。我想當我向管理員詢問「是ex090920.log是否正確的格式?」時,我應該詢問文件是否存在。

那麼,至少它已經解決了,因爲所有的頭撞都會(希望)沒有永久性的傷害。

感謝您的建議斯蒂芬和ongle。

0

如果服務器運行的是64位操作系統,那麼這個最有可能的原因是你的兩個過程,IIS和您的日誌處理服務,與不同的位數運行。

在64位機器文件系統重定向導致32位進程訪問system32實際訪問SysWOW64。所以如果你的一個進程以64位進程運行,另一個進程爲32位進程,他們在讀取或寫入system32時會實際訪問兩個不同的位置。

+0

我搜索了SysWOW64的c:驅動器並沒有找到它,所以我會認爲這是一個32位系統,除非SysWOW64在我無權訪問的目錄中。 – Scott 2009-09-21 15:03:53