0

我想从 ASP .NET 应用程序登录到应用程序事件日志。我想调用 EventLog.SourceExists 来确定事件源是否首先存在,如果不存在则创建它。

如 MSDN (http://msdn.microsoft.com/en-us/library/6s7642se.aspx) 中所述:“要在 Windows Vista 及更高版本或 Windows Server 2003 中搜索事件源,您必须具有管理权限。此要求的原因是必须搜索所有事件日志,包括安全性,以确定事件源是否唯一。从 Windows Vista 开始,用户没有访问安全性日志的权限;因此,抛出 SecurityException。 "

在我的例子中,NetworkService 用户没有管理员权限,所以 EventLog.SourceExists 会抛出 MSDN 中记录的 SecurityException。

我的问题是:如何围绕对 EventLog.SourceExists 的调用进行防御性编码?在调用 EventLog.SourceExists 之前,我可以先调用一些东西来检查我是否有权搜索事件源?

或者是我唯一的选择来捕获 SecurityException 并在 catch 块内采取一些其他操作,例如记录到现有事件源,例如信息

谢谢

凯文

4

1 回答 1

0

Do you absolutely have to write to the Windows application event log? The security restrictions that are in place by default do make this difficult to do. Every time I've been developing on a new VM I have to make a registry entry to allow the NETWORK_USER account to write to the log and as soon as you roll your application onto a live server (where making wholesale registry changes probably isn't a good idea) then you are snookered.

Also, experience has taught me that the Windows event log isn't the friendliest environment for logging / debugging. For server apps that run overnight I tend to do all my event logging to an html file that I can read the next day, for web apps I use a custom database and send all my events to that instead.

于 2012-05-01T11:41:37.387 回答