
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
464
|
Chapter 8: Diagnostics
// Get a list of all logs on the box.
string[] eventLogNames = Registry.LocalMachine.OpenSubKey
(@"SYSTEM\CurrentControlSet\Services\Eventlog").GetSubKeyNames( );
foreach (string log in eventLogNames)
{
// Get all the source names for this log.
List<string> sourceNamesList = FindSourceNamesFromLog(log)
// Add the source name list with the log name
// as the key to the hashtable.
logsAndSources.Add(log, sourceNamesList);
}
return logsAndSources;
}
This method returns a Hashtable with the log name as the key and a List<string> of
source names as the
Hashtable’s value. The information in the Hashtable of
List<string>s can be accessed using the following code:
foreach (DictionaryEntry DE in logsAndSources)
{
Console.WriteLine("Log: " + DE.Key); // Display the log.
foreach (string source in ((List<string>)DE.Value))
{
// Display all sources for this log.
Console.WriteLine("\tSource: " + source);
}
}
Discussion
This recipe is similar to Recipe 8.7 in that you need to find information concerning
an event log that can be obtained only through the registry. If you need to find the
sources associated with a log called
MyLog, you would look up all of the subkeys con-
tained in the following location:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\MyLog\ ...