This documentation is archived and is not being maintained.

Handling 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 .NET 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 procedure.

For more information on event handlers, see Event Handlers in Visual Basic and Visual C#.

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.
    Note   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 EventLog.EntryWritten Event 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:
    ' Visual Basic
    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: " & _
          Console.WriteLine("Entry written by another application. ")
       End If
    End Sub
    // C#
    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);
          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 EventLogEventHandler for your component that will call the EventLog1.EntryWritten procedure when an entry is written to the log. Your code should look like this:
    ' Visual Basic
    AddHandler EventLog1.EntryWritten, _
       New System.Diagnostics.EntryWrittenEventHandler( _
       AddressOf Me.EventLog1_EntryWritten)
    // C#
    this.eventLog1.EntryWritten += new
    Note   For more information on this syntax, see Event Handlers in Visual Basic and Visual C#.
  2. Create the EntryWritten procedure and define the code you want to process the entries.
  3. Set the EnableRaisingEvents property to true.

See Also

Event Handlers in Visual Basic and Visual C#| Configuring EventLog Component Instances | Configuring EventLog Component Instances