Write to the Windows Event Log

Problem

You need to write a log entry to the Windows event log on the current computer.

Solution

Register the event source and create the log if required. Write the message using the System.Diagnostics.EventLog class.

Discussion

The EventLog class makes it easy to write and retrieve event log entries. You create an EventLog instance that wraps a specific log—typically the all-purpose Application log or a custom log that’s used by your application.

' Access the Application log.
Dim Log As New EventLog("Application")

You can also supply an additional argument to the EventLog constructor to specify a computer name. This allows you to access an event log defined on another computer on the network.

' Access the Application log. ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.