创建新的事件日志源
问题描述:
我的应用程序调用一个库(我没有控制权)创建一个新的EventLog源并使用EventLog.SourceExists。它引发System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
创建新的事件日志源
该应用需要读访问HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Security
。如何将网络服务权限授予注册表(以编程方式)?
感谢您的指点。
答
您会收到此错误消息,因为您的“新来源”未注册,因此您需要管理权限。尝试在控制台中以“管理员”身份运行您的APP。
我曾经通过自己添加“Source”来破解“注册表”,但这可能不太合适。
答
我今天遇到了同样的问题,对于我的情况(非安装计划任务exe),没有任何WinForms或ASPX的答案似乎可行。所以我这样做: -
protected void prog_Load(object sender, EventArgs e)
{
boolean setupComplete = false;
try // setting an Event log entry, just to see if we can
{
logEvent = "prog started";
EventLog.WriteEntry(logSource, logEvent, EventLogEntryType.Information, 0);
setupComplete = true;
}
catch (Exception eLog1) // we can't, so try to fix
{
try
{
EventLog.CreateEventSource(logSource, logLog);
logEvent = "prog registered for Event Logging";
EventLog.WriteEntry(logSource, logEvent, EventLogEntryType.Information, 0);
}
catch (Exception eLog2) // aha! we probably lack admin rights to set the registry key
{
MessageBox.Show("prog needs admin rights the first time it runs", "prog Setup", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
// run
if (setupComplete == true)
{
DoTheWork();
}
// exit
this.Close();
}
我的应用程序不需要管理员权限,只需要安全分支的读访问权限。 – Sam 2012-07-16 20:05:06
只有拥有管理员或域权限(只读)的用户才能使用“设计”安全分支。以编程方式绕过这一点,您可能需要更改“注册表设置”。 KB附加:http://support.microsoft.com/kb/323076 – 2012-07-16 20:16:02