Search Amazon:

Create, read, write and delete event logs in VB.NET

In .NET the EventLog class from the System.Diagnostics namespace lets you read from existing logs, write entries to logs, create or delete event sources, delete logs, and respond to log entries. This can be useful when errors occur within your code. This code shows how to use the EventLog class. To learn how to add debugging and tracing capability to your application click here.

    Imports System.Diagnostics

    Dim aLog As EventLog
    Dim myLog As New EventLog
    Dim aEventLogList() As EventLog
    Dim aLogEntry As EventLogEntry
    Dim aLogEntries As EventLogEntryCollection
    '
    ' Create a new log.
    '
    If Not EventLog.SourceExists("MyNewSource") Then
        EventLog.CreateEventSource("MyNewSource", "MyNewLog")
    End If
    '
    ' Add an event to fire when an entry is written.
    '
    AddHandler myLog.EntryWritten, AddressOf OnEntryWritten

    With myLog
        .Source = "MyNewSource"
        .Log = "MyNewLog"
        .EnableRaisingEvents = True
        '
        ' Write a few entries.
        '
        .WriteEntry("Writing Error entry to event log", EventLogEntryType.Error)
        .WriteEntry("Writing Information entry to event", EventLogEntryType.Information)
        .WriteEntry("Writing Warning entry to event", EventLogEntryType.Warning)
        '
        ' Output all events in the new log.
        '
        aLogEntries = .Entries()
        For Each aLogEntry In aLogEntries
            With aLogEntry
                Console.WriteLine( _
                    "Source: {0}" & vbCrLf & _
                    "Category: {1}" & vbCrLf & _
                    "Message: {2}" & vbCrLf & _
                    "EntryType: {3}" & vbCrLf & _
                    "EventID: {4}" & vbCrLf & _
                    "UserName: {5}", _
                    .Source, .Category, .Message, .EntryType, .EventID, .UserName)
            End With
        Next
        '
        ' Delete your new log.
        '
        .Clear()
        .Delete("MyNewLog")
        '
        ' Output the names of all logs on the system.
        '
        aEventLogList = .GetEventLogs()
        For Each aLog In aEventLogList
            Console.WriteLine("Log name: " & aLog.LogDisplayName)
        Next
    End With


    Public Sub OnEntryWritten(ByVal source As Object, ByVal e As EntryWrittenEventArgs)
        Console.WriteLine(("Written: " + e.Entry.Message))
    End Sub 

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms