Export (0) Print
Expand All

How to: Configure EventLog Component Instances 

There are three major properties involved in configuring an instance of the EventLog component:

  • The Log property indicates the log with which you want to interact.

  • The MachineName property indicates the computer on which the log you are working with resides.

  • The Source property indicates the source string which will be used to identify your component when it writes entries to a log.

The way you configure your EventLog component instance depends on how you are planning to use it:

  • If you are planning to write entries to a log, you must do one of two things. Either you must register a source using the CreateEventSource method and set the EventLog component's Source property to that same source, or you must set the MachineName, Log, and Source properties for the component.

    NoteNote

    You can set the Log property on your component directly, or you can set it in code that registers your component as an event source. After registering the component as a source, a connection is already set in the server's registry linking your component to a specific log.

  • If you are planning to read log entries or monitor a log, you must set the MachineName and Log properties to indicate the log you are watching. You do not need to set the Source property in this case.

  • If you want your component to be able to receive notification of EntryWritten events, you must set EnableRaisingEvents to true. For more information, see How to: Handle the EntryWritten Event.

    The following example shows how to configure your component to allow you to write entries to the Application log. In this case, the source has not already been registered with the log, so you must specify the source string, log, and computer name. You do not need to explicitly call the CreateEventSource method because the WriteEntry call will register the source with the log if it has not already been registered.

    System.Diagnostics.EventLog EventLog1 = new
       System.Diagnostics.EventLog("Application", "myserver", "newsource");
    EventLog1.WriteEntry("Test");
    
    
    System.Diagnostics.EventLog EventLog1 = new
       System.Diagnostics.EventLog("Application", "myserver", "newsource");
    EventLog1.WriteEntry("Test");
    

    If the source has already been registered, you can simply set the Source property and write your entry. For example, the following code shows how to write an entry to a log using a source that has already been registered.

    if (!System.Diagnostics.EventLog.SourceExists("ExistingSourceString"))
        System.Diagnostics.EventLog.CreateEventSource(
           "ExistingSourceString", "Application");
    
    System.Diagnostics.EventLog EventLog1 =
       new System.Diagnostics.EventLog();
    EventLog1.Source = "ExistingSourceString";
    EventLog1.WriteEntry("TestEntry2");
    
    
    if (!System.Diagnostics.EventLog.SourceExists("ExistingSourceString"))
       {
          System.Diagnostics.EventLog.CreateEventSource(
             "ExistingSourceString", "Application");
       }
    System.Diagnostics.EventLog EventLog1 = 
       new System.Diagnostics.EventLog();
    EventLog1.set_Source("ExistingSourceString");
    EventLog1.WriteEntry("TestEntry2");
    
    NoteNote

    If you do not set the MachineName property, as shown in these examples, the local computer is assumed.

    For more information on these properties, see Event Log References.

    The entries you retrieve from an event log are categorized by multiple properties. For information on these properties, see EventLog.

To configure an instance of the EventLog component

  1. Create an instance of the EventLog component. For more information, see How to: Create EventLog Component Instances.

  2. Set the appropriate combination of the Log, MachineName, and Source properties:

    If you will be Do this

    Reading or monitoring event logs

    Set Log and MachineName

    Writing entries to a log using a new Source string

    Set Source, Log, and MachineName

    Writing entries to a log for an existing Source

    Set the Source property to the existing source; the other properties will be set automatically

  3. To enable your component instance to receive notifications of the EntryWritten event, set the EnableRaisingEvents property to true.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft