How to: Add Your Application as a Source of Event Log Entries

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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 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. In general, create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources and you attempt to write an event with the new source, the write operation will fail. If creating the source during installation is not an option, then try to create the source well ahead of the first write operation, perhaps during your application initialization. If you choose this approach, be sure your initialization code is running with administrator rights on the computer. These rights are required for creating new event sources.

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 How to: Create and Remove Custom Event Logs.

You can register the event source with localized resources for your event category and message strings. Your application can write event log entries using resource identifiers, rather than specifying the actual string values. Refer to the EventLogInstaller and EventSourceCreationData classes for more information on configuring your source with resource files.

To establish your component as an event source

  • To create entries on the local computer, 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:

    EventLog.CreateEventSource("MyApp1", "Application")
    
            System.Diagnostics.EventLog.CreateEventSource("MyApp1", "Application");
    

    Tip

    To create an event source on a remote computer, use EventSourceCreationData. The following code shows an example:

    Dim creationData As New EventSourceCreationData("ApplicationName", "Application")
    creationData.MachineName = "ServerName"
    EventLog.CreateEventSource(creationData)
    
            System.Diagnostics.EventSourceCreationData creationData = new
                System.Diagnostics.EventSourceCreationData("ApplicationName", "Application");
            creationData.MachineName = "ServerName";
            EventLog.CreateEventSource(creationData);
    

See Also

Tasks

How to: Determine If an Event Source Exists

How to: Remove an Event Source

How to: Create and Remove Custom Event Logs

Walkthrough: Exploring Event Logs, Event Sources, and Entries

Reference

EventLog

Concepts

Introduction to the EventLog Component