You are on page 1of 6

SUBMITTED TO:

Mam Shazia

Maham Butt
Name Maham Salar Butt
Roll# 151-97
Semester 6th
Section BSCS-B

VISUAL PROGRAMMING
EVENTLOG
VISUAL PROGRAMMING

Event Logging
Information is the cornerstone of our economy.
Information is also key to software development. It
is helpful to know what your application is doing as
you are assembling core components, during
testing, and after the application is shipped.This
information can be available to you at a modest
cost without implementing new tools. VB.NET
defines the EventLog class in the
System.Diagnostics namespace. Using the
EventLog class you can gather significant application information about your deployed
application, and you can create a new log, logging anything and everything you need while
you are building and testing.

A user may not be able to readily reproduce or describe what went wrong while using your
application, but if you have logged critical information in your deployed application, the user
can export and mail you a copy of the event log. If the testers where you work are performing
whitebox testing then they can mail you a copy of the custom log, enabling you to scrutinize
the behavior of your code in a testing environment.

Many applications record errors and events in proprietary error logs, each with their own
format and user interface. Data from different applications can't easily be merged into one
complete report, requiring system administrators or support representatives to check a
variety of sources to diagnose problems.

Event logging provides a standard, centralized way for applications (and the operating system)
to record important software and hardware events. The event logging service records events
from various sources and stores them in a single collection called an event log. The Event
Viewer enables you to view logs; the programming interface also enables you to examine logs.

About event logging


When an error occurs, the system administrator or support representative must determine
what caused the error, attempt to recover any lost data, and prevent the error from recurring.
It is helpful if applications, the operating system, and other system services record important
events, such as low-memory conditions or excessive attempts to access a disk. The system
administrator can then use the event log to help determine what conditions caused the error
and identify the context in which it occurred. By periodically viewing the event log, the system
page.

1
VISUAL PROGRAMMING

administrator may be able to identify problems (such as a failing hard disk) before they cause
damage.

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.

ImportsSystem
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 = "."
page.

2
VISUAL PROGRAMMING

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
page.

sSource = "dotNET Sample App"

3
VISUAL PROGRAMMING

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

Sample code

' check if it does not already exist


If Not EventLog.SourceExists("ToolboxLessons") Then
' create the source
EventLog.CreateEventSource("ToolboxLessons", "LessonsLog")
Console.WriteLine("Creating Event Source...")
' the source is created so Exit the application to allow it to be registered.
Application.Exit()
End If

' instantiate an EventLog and assign the source


Dim LessonsLog As New EventLog()
LessonsLog.Source = "ToolboxLessons"

' write an informational entry to the event log.


LessonsLog.WriteEntry("Writing to the event log...")
page.

4
VISUAL PROGRAMMING

Refrences

https://msdn.microsoft.com/en-us/library/windows/desktop/aa363652(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/aa363632(v=vs.85).aspx

https://support.microsoft.com/en-us/help/301279/how-to-write-to-an-event-log-by-using-
visual-basic-net-or-visual-basic

http://www.visual-basic-tutorials.com/Tutorials/Controls/EventLog.html

https://www.codeguru.com/columns/vb/article.php/c6561/Using-the-EventLog-Class-in-
VBNET.htm

page.

You might also like