Share via


How to: Handle the EntryWritten EventĀ 

You can create event handlers for your EventLog components that automatically call a procedure when an entry is written to a log. There are several ways you can create an event handler for an instance of the EventLog component, but the simplest way is to let Visual Studio handle most of the syntax for you automatically. When you double-click your EventLog component in the designer, Visual Studio moves you to the Code Editor and creates the event handler and the blank procedure it calls. You can then fill in the processing that you want to occur in the EntryWritten event handler.

For more information on event handlers, see Handling and Raising Events.

To create a default handler for the EntryWritten event

  1. From the designer, double click on the EventLog component for which you want to create a handler.

    NoteNote

    The Code Editor appears and two items are added to your code: the handler that creates and registers a delegate and calls the procedure, and a blank procedure for the EntryWritten event.

  2. In the blank procedure for the EntryWritten event, define code to receive and process entries when this event is called. Your code might look like this:

    Private Sub EventLog1_EntryWritten(ByVal sender As System.Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs) Handles EventLog1.EntryWritten
        If e.Entry.Source = "MyApplication" Then
            Console.WriteLine("Entry written by my app. Message: " & _
               e.Entry.Message)
        Else
            Console.WriteLine("Entry written by another application. ")
        End If
    End Sub
    
    private void eventLog1_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
    {
        if (e.Entry.Source == "MyApplication")
            Console.WriteLine("Entry written by my application. Message: "
               + e.Entry.Message);
        else
            Console.WriteLine("Entry was written by another application.");
    }
    
    private void eventLog1_EntryWritten(System.Object sender, 
       System.Diagnostics.EntryWrittenEventArgs e)
    {
       if ( e.get_Entry().get_Source() == "MyApplication"  )
       {
          Console.WriteLine("Entry written by my application. Message: "
             + e.get_Entry().get_Message());
       }
       else
       {
          Console.WriteLine("Entry was written by another application.");
       }
    }
    
  3. Set the EnableRaisingEvents property to true.

To create a handler programmatically

  1. Use the AddHandler method to create an event handler of type EntryWrittenEventHandler for your component that will call the eventLog1_EntryWritten method when an entry is written to the log. Your code should look like this:

    Public Sub method5()
        AddHandler EventLog1.EntryWritten, _
           New System.Diagnostics.EntryWrittenEventHandler( _
           AddressOf Me.EventLog1_EntryWritten)
    
    this.eventLog1.EntryWritten += new
       System.Diagnostics.EntryWrittenEventHandler(
       this.eventLog1_EntryWritten);
    
    this.eventLog1.add_EntryWritten(new 
       System.Diagnostics.EntryWrittenEventHandler(
          this.eventLog1_EntryWritten));
    
    NoteNote

    For more information on this syntax, see Handling and Raising Events.

  2. Create the EntryWritten procedure and define the code you want to process the entries.

  3. Set the EnableRaisingEvents property to true.

See Also

Tasks

How to: Configure EventLog Component Instances
How to: Configure EventLog Component Instances

Other Resources

Handling and Raising Events