Be a programmer

Live with passion....
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

快速获取事件日志中注册的所有事件源

Posted on 2006-03-04 21:13  Programmer  阅读(1730)  评论(0编辑  收藏  举报

为我们的程序添加日志功能,使用 .net framework 中的 EventLog 类应该是一个不错的选择。在 .net framework 2.0 中,microsoft 又进一步丰富了 EventLog 类,使我们操作 windows 中的事件日志更方便。

在操作事件源的时候,EventLog 类提供了三个方法:CreateEventSource, DeleteEventSource, SourceExists。这些方法用于一般的事件日志操作已经足够了。但是如果需要列举出某个日志中所有已经注册的事件源,可能就需要我们自己想办法了。.net framework 中似乎并没有这个功能。不知道 microsoft 是出于什么样的考虑而不提供这个功能呢?安全原因,还是因为各个 windows 版本之间对事件日志实现的差异?

使用 .NET Reflector 查看 System.dll 的源代码,可以看到每次创建事件源的时候,都会更新一个名为 Sources 的注册键值( 注册项的名称为 HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ EventLog \ Log name )。因此,只要读取 Sources 的值就可以获取日志所有注册的事件源了。

代码如下:

  • public static string[] GetLogSources(string logName)
  • {
  • if (logName == null || logName.Length == 0) {
  • return new string[0];
  • }
  •  
  • RegistryKey localMachineKey = null;
  • RegistryKey logKey = null;
  • string[] sources = null;
  •  
  • try {
  • // The detail information about a log can be found at the registry.
  • // And the registry key for a log is:
  • // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\
  • //
  • string path = string.Concat(@"SYSTEM\CurrentControlSet\Services\Eventlog\", logName);
  •  
  • localMachineKey = Registry.LocalMachine;
  • logKey = localMachineKey.OpenSubKey(path);
  • if (logKey != null) {
  • sources = logKey.GetValue("Sources") as string[];
  • }
  • }
  • finally {
  • if (logKey != null) {
  • logKey.Close();
  • }
  •  
  • if (localMachineKey != null) {
  • localMachineKey.Close();
  • }
  • }
  •  
  • return (sources == null) ? new string[0] : sources;
  • }