2014-04-01 41 views
0

執行未經授權的操作錯誤我有一個Windows服務在C#它創建一個事件日誌中,像這樣:試圖在訪問事件日誌

public partial class WizardService : ServiceBase 
{ 
    public WizardService() 
    { 
     InitializeComponent(); 

     // Setup logging 
     if (!EventLog.SourceExists("WizardService")) 
     { 
      EventLog.CreateEventSource(
       "WizardService", "WizardServiceLog"); 
     } 

     eventLog1.Source = "WizardService"; 
     eventLog1.Log = "WizardServiceLog"; 
    } 

Windows服務可以成功訪問該事件日誌。 Windows服務被配置爲以本地服務運行。這需要在Win7和Windows Server 2008 R2上運行。

但是,該服務還會向ASMX Web服務發出呼叫。我想ASMX Web服務也可以訪問相同的事件日誌,但我得到這個錯誤:

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. 
    at Microsoft.Win32.RegistryKey.Win32ErrorStatic(Int32 errorCode, String str) 
    at Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive hKey, String machineName, RegistryView view) 
    at System.Diagnostics.EventLogInternal.GetEventLogRegKey(String machine, Boolean writable) 
    at System.Diagnostics.EventLogInternal.FindSourceRegistration(String source, String machineName, Boolean readOnly, Boolean wantToCreate) 
    at System.Diagnostics.EventLogInternal.SourceExists(String source, String machineName, Boolean wantToCreate) 
    at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) 
    at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) 
    at System.Diagnostics.EventLog.WriteEntry(String message) 

這裏就是ASMX Web服務使用代碼:

public class WizardService : WebService 
{ 
    private EventLog eventLog; 

    public WizardService() 
    { 
     eventLog = new EventLog("WizardServiceLog", "localhost", "WizardService"); 
    } 

    private SomeOtherMethod() 
    { 
     eventLog.WriteEntry("Error : " + ex.Message + Environment.NewLine + ex.StackTrace); 
    } 
} 

錯誤發生在eventLog.WriteEntry()行。

回答

0

這可能是因爲您的Web服務正在運行的AppPool沒有足夠的權限來訪問事件日誌。

最佳做法是創建一個用戶,該用戶擁有適當的訪問權限但不允許所有內容。如果這樣,有人黑客你的服務,他們沒有完整權利等等

然而,這一說,開始與分配Network ServiceFull Admin Rights用戶對AppPool和檢查事件日誌的訪問,然後調整權限需要。