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