System Event Logs
You can find the existing logs on a computer by using the GetEventLogs shared method of the EventLog class.
The GetEventLogs method searches for all event logs on the local computer, and then it creates an array of EventLog objects that contain the list.
The following code example retrieves a list of logs on the local computer, and then displays the names of the logs in a console window:
Dim remoteEventLogs() As EventLog
'Gets logs on the local machine, give remote machine name to get the logs on the remote machine
remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName)
Console.WriteLine("Number of logs on computer: " & remoteEventLogs.Length)
'Display the list of event logs
Dim log As EventLog
For Each log In remoteEventLogs
Console.WriteLine("Log: " & log.Log)
Next log
Read and Write Logs to and from the Local and the Remote System
Read logs
To read an event log, use the Entries property of the EventLog class.
The Entries property of the EventLog class is a collection of all the entries in the event log.
The following code example demonstrates how to iterate through this collection, and how to read all the entries in the specified log:
' Log type can be Application, Security, System or any other custom log
' Select the log type you want to read
Dim logtype As String = "Application"
' In the constructor of the eventlog, pass the log type and the computer name
' from which you want to read the logs
Dim evtLog As New EventLog(logtype, System.Environment.MachineName)
Dim lastlogtoshow As Integer = evtLog.Entries.Count
If lastlogtoshow <= 0 Then
Console.WriteLine("There are no event logs in the log : " & logtype)
Exit Sub
End If
' Read the last record in the specified log
Dim currentEntry As EventLogEntry
Dim i As Integer
' Show Last 2 entries. You can similarly write the log to a file.
For i = evtLog.Entries.Count - 1 To lastlogtoshow - 2 Step -1
currentEntry = evtLog.Entries(i)
Console.WriteLine("Event Id is : " & currentEntry.EventID)
Console.WriteLine("Entry type is : " & currentEntry.EntryType.ToString())
Console.WriteLine("Message is : " & currentEntry.Message & vbCrLf)
Next
evtLog.Close()
Write Logs
To write an event log, use the WriteEntry method of the EventLog class.
To write the event log successfully, make sure your application has write access for the log that it is writing to.
For more information about the permissions that you must have to read and write in event logs, visit the following Microsoft Web site.
You must set the Source property on your EventLog component instance before you write entries to a log.
When your component writes an entry, the system automatically verifies that the source you specified is registered with the event log that the component is writing to.
The system then calls CreateEventSource if necessary.
To write an event log, you must pass the machine name where the log resides.
In the following code example, the MachineName property of the Environment class determines the name of the local machine:
' Check if the source exists
If Not EventLog.SourceExists("MySystemSource", System.Environment.MachineName) Then
EventLog.CreateEventSource("MySystemSource", "System", System.Environment.MachineName)
End If
Dim evtLog As New EventLog("System", System.Environment.MachineName, "MySystemSource")
'writing to system log, in the similar way you can write to other
'logs for which you have appropriate permissions to write
evtLog.WriteEntry("warning is written to system log", EventLogEntryType.Warning, CInt(10001))
Console.WriteLine("Log written to the system log.")
evtLog.Close()
Clear Logs
When an event log is full, it stops recording new event information or it begins to overwrite previous entries.
If event recording stops, you can clear the log of existing entries and allow it to start recording events again.
To clear event log entries, you must have administrator permissions for the computer that the log resides on.
Call the Clear method on the EventLog component instance.
The following code example domonstrates how to clear a log:
' Create an EventLog instance and pass log name and MachineName on which the log resides
Dim evtLog As New EventLog("Security", System.Environment.MachineName)
evtLog.Clear()
evtLog.Close()
Comments
Post a Comment