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

为我们的程序添加日志功能,使用 .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;
  • }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值