试图在.NET中使用EventLog.SourceExists方法时问题方法、问题、NET、EventLog

2023-09-03 15:57:15 作者:难以言语的伤痛

我想使用使用C#在我的应用eventlogs,所以我增加了以下code

I am trying to use eventlogs in my application using C#, so I added the following code

if (!EventLog.SourceExists("SomeName"))
EventLog.CreateEventSource("SomeName", "Application");

在EventLog.SourceExists导致SecurityException异常,说 该人士未找到,但全部或部分事件日志不能被搜索无法访问的日志:。安全

The EventLog.SourceExists causes SecurityException that says "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security."

我以管理员身份运行在Windows 7中。

I am running as administrator in Windows 7.

任何帮助将appriciated。

Any help would be appriciated.

推荐答案

这是一个权限的问题 - 你应该给正在运行的用户权限读取以下注册表项:

This is a permissions problem - you should give the running user permission to read the following registry key:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog

Alternaitvely可以bypas的为CreateEventSource 不再需要访问此注册表项。

Alternaitvely you can bypas the CreateEventSource removing the need to access this registry key.

这两种解决方案中更详细在下面的螺纹解释 - How做我创建一个事件日志源在Vista?。

Both solutions are explained in more detail in the following thread - How do I create an Event Log source under Vista?.