如何:确定事件源是否存在
更新:2007 年 11 月
在为某特定事件日志标识一个源后,此信息就存储在服务器的注册表文件中,直到您移除此源为止。如果尝试再注册一个已注册为某一给定日志的有效源的源,系统会产生运行时错误。可以使用 SourceExists 方法确定某一特定源是否已注册。
确定源是否已注册
调用 SourceExists 方法,指定要查询的源名称。
下面的示例说明了如何确定一个使用字符串 MyApp1 的源是否已注册,以及如果未注册,怎样将它注册到 Application 日志。
If Not EventLog.SourceExists("MyApp1") Then EventLog.CreateEventSource("MyApp1", "Application") End If
if (!System.Diagnostics.EventLog.SourceExists("MyApp1")) System.Diagnostics.EventLog.CreateEventSource( "MyApp1", "Application");
要确定一个源是否已在一个远程计算机上注册,请将该计算机名指定为第二个参数。以下代码提供了一个示例:
If Not EventLog.SourceExists("MyApp1", "myserver") Then Dim create As New EventSourceCreationData("MyApp1", "Application") create.MachineName = "myserver" EventLog.CreateEventSource(create) End If
EventSourceCreationData sourceData = new EventSourceCreationData("MyApp1", "Application"); sourceData.MachineName = "myserver"; if (!System.Diagnostics.EventLog.SourceExists("MyApp1", "myserver")) System.Diagnostics.EventLog.CreateEventSource(sourceData);