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

Article translations Article translations
Article ID: 301279 - View products that this article applies to.
This article was previously published under Q301279
Expand all | Collapse all

On This Page

SUMMARY

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

Requirements

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

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 System
    Imports 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 String
    Dim sLog As String
    Dim sEvent As String
    Dim sMachine as String
    
    sSource = "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.

Complete Code Listing

Imports System
Imports System.Diagnostics

Module 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 Sub

End Module

Properties

Article ID: 301279 - Last Review: December 6, 2006 - Revision: 5.3
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbvs2005applies kbvs2005swept kbhowtomaster KB301279

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com