Microsoft KB Archive/301279

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

Article ID: 301279

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft 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

IN THIS TASK
SUMMARY
 * Requirements
 * Write to an Event Log
 * Complete Code Listing



SUMMARY
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

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

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:  Open Visual Studio .NET or Visual Studio 2005 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. Make sure that at least the System namespace is referenced by the project.  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   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 = &quot;dotNET Sample App&quot; sLog = &quot;Application&quot; sEvent = &quot;Sample Event&quot; sMachine = &quot;.&quot; </li>  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                   </li>  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)) </li> Save and run your code, and then check the Application log in the Event Viewer to see your new events.</li> 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.</li></ol>

back to the top

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 = &quot;dotNET Sample App&quot; sLog = &quot;Application&quot; sEvent = &quot;Sample Event&quot; sMachine = &quot;.&quot;

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 back to the top

Keywords: kbvs2005applies kbvs2005swept kbhowtomaster KB301279

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.