다음을 통해 공유


EventLog Programming Architecture 

The EventLog component uses these portions of the .NET Framework namespaces:


EventLog Namespace

Event Log Namespace

When you add an instance of the EventLog component to your application in Visual Studio, the system automatically creates the references and import statements you need to access the namespace and classes. However, if you are creating your EventLog components in code or in the Microsoft .NET Framework, you might need to create a reference to System.Diagnostics and add a statement to your project importing the same library.

For instructions on adding and removing project references, see How to: Add or Remove References in Visual Studio.

Key Programming Elements in the EventLog Class

The EventLog component raises an event called EntryWritten whenever an entry is written to the log you are watching. You can define a handler for this event. In addition, you can use the following methods to interact with an instance of the EventLog component:

  • You can use the Exists method to determine whether a particular event log exists.

  • You can use the CreateEventSource method to define a string that creates a relationship between a source of events and a log, and use the DeleteEventSource method to remove this association. You can also use this method to create new custom event logs. Just type a new log name as the log in which you want to create your source, and the system will create the log automatically.

    NoteNote

    You can set the Source property to any string; it does not have to correspond to the name of your application. A single application can contain multiple components that use different source strings to identify themselves to event logs.

  • You can use the WriteEntry method to write event information to any event log to which you have access. The WriteEntry method automatically registers your component or application as a valid source if it is not already registered with the designated log.

  • You can use the SourceExists method to determine whether a particular source is already registered. This is useful when you are attempting to create a new source on a given computer, because the SourceExists method will cause a run-time error if the source is already registered on that computer.

  • You can use the GetEventLogs method to retrieve a list of all the event logs on a particular computer.

  • You can use the Delete method to delete an entire event log. Use this method with caution, as deleting a log will also delete any sources registered in it.

See Also

Concepts

Introduction to the EventLog Component