다음을 통해 공유


How to: Determine If Specific Event Logs Exist 

You can use the 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 SourceExists method, to determine if a particular source string exists. See How to: Determine If an Event Source Exists for more information.

NoteNote

Because the Exists and SourceExists methods are Shared, 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 computers 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 computer. This code assumes that an Imports or using statement exists for the System.Diagnostics namespace:

    Dim logExists As Boolean
    logExists = EventLog.Exists("Application")
    
    bool logExists = System.Diagnostics.EventLog.Exists("Application");
    
    boolean 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."

    NoteTip

    You can specify a computer name as a second parameter to determine if an event log exists on a remote computer.

    NoteNote

    Application is case sensitive.

See Also

Tasks

How to: Create and Remove Custom Event Logs
Walkthrough: Exploring Event Logs, Event Sources, and Entries

Concepts

Security Ramifications of Event Logs

Other Resources

Administering Event Logs