Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Create an Event Log, Event Sources and Entries
Download Source Code : 290_LogFiles.zip

In this sample application I will try to explain you how you can create an Event Log, Event Sources and Entries in VS2005.

Event log is used to record information about the software and hardware events. Through these event logs you can access and customize the windows events. The method used for accessing and customizing the windows events are read, write, delete, custom and many more. You will see these function’s functionalities in sample program.

 

To read from log, you need to specify the “log name” and machine name for the Event Log. Source entry in reading from event log is optional; source name is only required when you are writing log. Source name for every event is distinct this means that you can only use source name once for creating new event you need another source name.

 

To get more understanding about the Event log consider a following example that performs eight operations that are, Create Log, Delete Log, Write Entry, Clear Log, Verify Log Exists, Verify Source Exists, Remove Event Source, Read Entry. To accomplish these task you need to design a windows form that contain one label, one multi-line textbox control and eight button control. Import System.Diagnostics namespace and paste following code in you Form1.vb file:

 

Double click on the Custom log button and add the following code:

 

Private Sub cmdCustomLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustomLog.Click

 

If EventLog.SourceExists("ELSrc") Then

      EventLog.DeleteEventSource("ELSrc")

End If

 

EventLog.CreateEventSource("ELSrc", "ELName")

EventLog1.Log = "ELName"

EventLog1.Source = "ELSrc"

txtShowLogDetail.Text = "NEW: " & EventLog1.Log & EventLog1.Source

 

End Sub

Double click on the Delete log button and add the following code:

 

Private Sub cmdDeleteLog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdDeleteLog.Click

If EventLog.Exists("ELName") Then

      EventLog.Delete("ELName")

      txtShowLogDetail.Clear()

End If

End Sub

Double click on the Write Entry button and add the following code:

 

Private Sub cmdWriteEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdWriteEntry.Click

If EventLog.SourceExists("ELSrc") = False Then

      EventLog.CreateEventSource("ELSrc", "ELName")

      EventLog1.Log = "ELName"

      EventLog1.Source = "ELSrc"

      txtShowLogDetail.Text = EventLog1.Log & EventLog1.Source

      EventLog1.WriteEntry("Your Event log is created :)")

Else

      EventLog1.WriteEntry("You Event Already Exists")

End If

End Sub

Double click on the Clear log button and add the following code:

 

Private Sub cmdClearLog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdClearLog.Click

      EventLog1.Clear()

      txtShowLogDetail.Clear()

End Sub

Double click on the Verify Log Exists button and add the following code:

 

Private Sub cmdVLExists_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdVLExists.Click

 

 Dim logExists As Boolean = EventLog.Exists("ELName")

 lblShowMsg.Text = "Does the log exist? " & logExists.ToString()

 

End Sub

Double click on the Verify Source Exists button and add the following code:

 

Private Sub cmdVSExists_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdVSExists.Click

 

 Dim sourceExists As Boolean = EventLog.SourceExists("ELSrc")

 lblShowMsg.Text = "Does the source exist? " + sourceExists.ToString()

 

End Sub

Double click on the Remove Existing Source button and add the following code:

 

Private Sub cmdRESource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRESource.Click

 If EventLog.SourceExists("ELSrc") Then

      EventLog.DeleteEventSource("ELSrc")

      txtShowLogDetail.Text = EventLog1.Log

End If

End Sub

Double click on the Read Entry button and add the following code:

 

Private Sub cmdReadEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdReadEntry.Click

 

Dim ELEntry As EventLogEntry

If txtShowLogDetail.Text = String.Empty Then

      lblShowMsg.Text = "You Must First Create a Log!"

Else

 

If EventLog1.Entries.Count > 0 Then

For Each ELEntry In EventLog1.Entries

      txtShowLogDetail.Text = ELEntry.Message

Next

Else

      lblShowMsg.Text = "No Log Entry Exists :("

End If

End If

End Sub                  

Article Comments
Hi, your article is very interesting, but i d like if there are possible to do that with vs2003 ! i use this program !

i m a young programmer if you are some idea i want that !

thanks !

christophe

Posted on 5/30/2006 9:22:49 AM by christophe

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use Delegates to Pass Procedure to another Procedure

Disclaimer - Privacy
© 2002-2017 DevASP.net