Determining If Specific Event Logs Exist
You can use the EventLog.Exists method to determine whether a specific log exists. If the log exists, the method returns true. You might use this method if you are creating a custom log and want to make sure that a log by that name does not already exist.
You must have registry rights in order to find out if a log exists. If you do not have the appropriate rights, the query will always return false. For information on setting registry rights, see your Windows documentation.
In addition to the Exists method, you can use a similar method, the EventLog.SourceExists method, to determine if a particular source string exists. See Determining If an Event Source Exists for more information.
Note Because the Exists and SourceExists methods are static, you do not need to create an instance of the EventLog component before you call either method — instead, you can call the method on the EventLog class itself.
You must have appropriate access rights on the remote machines to create and delete logs. For more information, see Security Ramifications of Event Logs.
To determine if an event log exists
- Call the Exists method, specifying the log on the local computer that you want to verify as a parameter.
The following example shows how to determine whether the Application log exists on the local machine. This code assumes that an Imports or Using statement exists for the System.Diagnostics namespace:
' Visual Basic Dim logExists As Boolean logExists = EventLog.Exists("Application") // C# bool logExists = System.Diagnostics.EventLog.Exists("Application");
If the log exists, the message box will show "True." If it does not exist, the message box will show "False."Tip You can specify a computer name as a second parameter to determine if an event log exists on a remote computer.Note Application is case sensitive.