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
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 EntryWritten event.
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."); }
Set the EnableRaisingEvents property to true.
To create a handler programmatically
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);
Note
For more information on this syntax, see Handling and Raising Events.
Create the EntryWritten procedure and define the code you want to process the entries.
Set the EnableRaisingEvents property to true.
See Also
Tasks
How to: Configure EventLog Component Instances
How to: Configure EventLog Component Instances