
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Finding All Sources Belonging to a Specific Event Log
|
463
See Also
See the “Handling the EntryWritten Event” and “EventLog.EntryWritten Event” top-
ics in the MSDN documentation.
8.10 Finding All Sources Belonging
to a Specific Event Log
Problem
You need to determine which sources are attached to a particular event log before
the log is examined and/or deleted. A source is a component or application that has
registered itself to a particular event log as a source of events.
Solution
Use the following method to extract all of the source names registered to a log (pass
the log’s name in as the
logName argument):
public static List<string> FindSourceNamesFromLog(string logName)
{
List<string> sourceNamesList = new List<string>( );
// Get the registry key for the specific log.
RegistryKey keyLog = Registry.LocalMachine.OpenSubKey
(@"SYSTEM\CurrentControlSet\Services\Eventlog\" + logName);
if (keyLog != null && keyLog.SubKeyCount>0)
{
// Get the sources from the log key.
string[] sourceNames = keyLog.GetSubKeyNames( );
// Set capacity for the list.
sourceNamesList.Capacity = keyLog.SubKeyCount;
// Add all of the sources into the list.
sourceNamesList.AddRange(sourceNames);
}
// Return the list.
return sourceNamesList;
}
To obtain a listing of all logs and their registered sources, use the following ...