You are currently offline, waiting for your internet to reconnect

How to write to an event log by using Visual Basic .NET or Visual Basic 2005

This article was previously published under Q301279
For a Microsoft Visual C# .NET version of this article, see 307024.
For a Microsoft Visual Basic 6.0 version of this article, see 154576.
For a Microsoft Visual C++ .NET version of this article, see 815661.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Diagnostics


This article demonstrates how to add your own entries to the operating system's event log using the Microsoft .NET Framework.

back to the top


The following list describes the recommended hardware, software, network infrastructure, and service packs that you will need:
  • Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
back to the top

Write to an Event Log

Event logging provides a standard, centralized way for your applications to record important software and hardware events. Windows supplies a standard user interface for viewing the logs, the Event Viewer. Using the common language's run-time EventLog component, you can easily connect to existing event logs on both local and remote computers, and write entries to these logs. You can also read entries from existing logs and create your own custom event logs. In its simplest form, writing to an event log involves only a few steps to create a sample application:
  1. Open Visual Studio .NET or Visual Studio 2005
  2. Create a new Console Application in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005. Visual Basic .NET or Visual Basic 2005 creates a Module for you, along with an empty Main() procedure.
  3. Make sure that at least the System namespace is referenced by the project.
  4. Use the Imports statement on the System and System.Diagnostics namespaces so that you will not be required to qualify declarations from these namespaces later in your code. These statements must be used prior to any other declarations.
    Imports SystemImports System.Diagnostics					
  5. To write to an event log, you need several pieces of information: Your message, the name of the log you wish to write to (which will be created if it does not already exist), and a string representing the source of the event. A particular source can be registered with only a single event log, so if you wish to write messages to more than one log you will need to define multiple sources.
    Dim sSource As StringDim sLog As StringDim sEvent As StringDim sMachine as StringsSource = "dotNET Sample App"sLog = "Application"sEvent = "Sample Event"sMachine = "."					
  6. Given all of this information, the first step is to use two static methods of the EventLog class to first check whether your source exists, and if not, to create this source associated with a particular event log. If the log name that you specify does not exist, it will be created automatically when you write your first entry to it. If you do not supply a log name to the CreateEventSource procedure, it will default to the Application log.
    If Not EventLog.SourceExists(sSource, sMachine) Then    EventLog.CreateEventSource(sSource, sLog, sMachine)End If					
  7. To write a message into an event log, you can create a new EventLog object and use the WriteEntry method which has several different overloaded versions. The simplest method, which takes your message, and one of the more complex ones that supports specifying the event ID and event type, are shown in the code below.
    Dim ELog as new Eventlog(sLog, sMachine, sSource)ELog.WriteEntry(sEvent)ELog.WriteEntry(sEvent, EventLogEntryType.Warning, 234, ctype(3,short))
  8. Save and run your code, and then check the Application log in the Event Viewer to see your new events.
  9. To write to an event log on a remote machine, simply change the the sMachine variable to a machine name that you have privileges to write to the event log on.
back to the top

Complete Code Listing

Imports SystemImports System.DiagnosticsModule Module1    Sub Main()        Dim sSource As String        Dim sLog As String        Dim sEvent As String        Dim sMachine as String        sSource = "dotNET Sample App"        sLog = "Application"        sEvent = "Sample Event"        sMachine = "."        If Not EventLog.SourceExists(sSource, sMachine) Then            EventLog.CreateEventSource(sSource, sLog, sMachine)        End If        Dim ELog as new Eventlog(sLog, sMachine, sSource)        ELog.WriteEntry(sEvent)        ELog.WriteEntry(sEvent, EventLogEntryType.Warning, 234, ctype(3,short))    End SubEnd Module
back to the top

Article ID: 301279 - Last Review: 12/06/2006 23:18:25 - Revision: 5.3

Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Visual Basic .NET 2003 Standard Edition, Microsoft Visual Basic 2005

  • kbvs2005applies kbvs2005swept kbhowtomaster KB301279