2014-04-01 39 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和检查事件日志的访问,然后调整权限需要。