This documentation is archived and is not being maintained.

Adding Your Application as a Source of Event Log Entries

Before your component can write an entry to an event log, you must register it with the event log as a valid source of events. You can do this by using the EventLog.CreateEventSource method and specifying a string that uniquely identifies your component to the event log. When you write a log entry, the system uses the source you identified to find the appropriate log in which to place your entry. Your EventLog component instance can write to only a single log at a time.

Note   By default, if you try to write an entry without first having registered your component as a valid source, the system automatically registers the source with the event log, using the value of the Source property as the source string.

If you specify the name of a log that does not exist when you use the CreateEventSource method, the system creates a new, custom event log for you with that name the first time you try to write an entry to the log. For more information, see Creating and Removing Custom Event Logs.

To establish your component as an event source

  • To create entries on the local machine, call the CreateEventSource method and specify the source string and log name as parameters.
    Note   If you specify null (" ") for the log name, it defaults to Application.

    The following example shows how to register your component MyApp1 as a source for the Application log. This code assumes that an Imports or Using statement exists for the System.Diagnostics namespace:

    ' Visual Basic
    EventLog.CreateEventSource("MyApp1", "Application")
    // C#
    System.Diagnostics.EventLog.CreateEventSource("MyApp1", "Application");
    Tip   To create an event source on a remote computer, specify the machine name as a third parameter. The following code shows an example:
    ' Visual Basic
    EventLog.CreateEventSource("MyApp1", "Application", "myserver")
    // C#
       "MyApp1", "Application", "myserver");

See Also

Determining If an Event Source Exists | Removing an Event Source | Introduction to the EventLog Component | Creating and Removing Custom Event Logs | Walkthrough: Exploring Event Logs, Event Sources, and Entries