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()行。