Results 1 to 5 of 5

Thread: View specific Event Log entry

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    View specific Event Log entry

    Hi.
    I’m working on getting certain Event Log Entries from the Event Log.
    What I’d like to achieve is:
    A user can select which Event Log Entry Type he wants to view. E.g: “Error”, “Warning”, “Information” etc…
    He should also be able to choose from which category he wants the logs. E.g: “System”, “Application” etc…
    After he has selected, only the Last 10 entries of that (selected) type should be saved to a text file.

    Help is really appreciated. If you want I can upload the whole Project.

    I have really no idea how to achieve this, but I did get the following code from a website which gets the event log entries and outputs to a rich textbox, but It doesn’t achieve what I want to.

    Code:
    Public Class ReadForm
        ' Stores the name of the log that the user wants to view.
        Private logType As String = ""
    
        Private Sub cmdViewLogEntries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewLogEntries.Click
            Try
                Const EntriesToDisplay As Integer = 10
    
                ' In this case the EventLog constructor is passed a string variable for the log name.
                ' This is because the user of the application can choose which log they wish to view 
                ' from the listbox on the form.
                Dim ev As New EventLog(logType, System.Environment.MachineName, "Event Log Sample")
    
                rchEventLogOutput.Text = "Event log entries (maximum of 10), newest to oldest." & vbCrLf & vbCrLf
    
                Dim lastLogToShow As Integer = ev.Entries.Count - EntriesToDisplay
                If lastLogToShow < 0 Then
                    lastLogToShow = 0
                End If
    
                ' Display the last 10 records in the chosen log.
                For index As Integer = ev.Entries.Count - 1 To lastLogToShow Step -1
                    Dim CurrentEntry As EventLogEntry = ev.Entries(index)
                    'rchEventLogOutput.Text &= "Event ID : " & CurrentEntry.InstanceId & vbCrLf
                    rchEventLogOutput.Text &= "Event ID : " & CurrentEntry.EntryType.Error & vbCrLf
                    rchEventLogOutput.Text &= "Entry Type : " & EventLogEntryType.Error.ToString() & vbCrLf
                    rchEventLogOutput.Text &= "Message : " & CurrentEntry.Message & vbCrLf & vbCrLf
                Next
            Catch secEx As System.Security.SecurityException
                MsgBox("Security exception in reading the event log.", MsgBoxStyle.OKOnly, Me.Text & " Error")
            Catch ex As Exception
                MsgBox("Error accessing logs on the local machine.", MsgBoxStyle.OKOnly, Me.Text & " Error")
            End Try
        End Sub
    
        Private Sub lstEntryType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEntryType.SelectedIndexChanged
            ' Store the log that the user selected in the ListBox
            logType = CType(lstEntryType.Items(lstEntryType.SelectedIndex()), String)
        End Sub
    
        Private Sub ReadForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                For Each currentLog As EventLog In EventLog.GetEventLogs()
                    lstEntryType.Items.Add(currentLog.LogDisplayName)
                Next
                lstEntryType.SelectedIndex = 0
            Catch secEx As System.Security.SecurityException
                MsgBox("Security exception in reading the event log.", MsgBoxStyle.OKOnly, Me.Text & " Error")
            Catch ex As Exception
                MsgBox("Error accessing logs on the local machine.", MsgBoxStyle.OKOnly, Me.Text & " Error")
            End Try
        End Sub
    
    End Class

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: View specific Event Log entry

    Specifically what does the code you got doesn't do that you would like it to do ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: View specific Event Log entry

    Thanks for replying back.
    The code I got doesn't get accurate results.

    When I edit the code to give me Event Log Entries for "Error", it doesn't give me what I see in the Event Viewer.

    If It helps here's the link to the sample project.

    PS: I do not own it, I found it on the Internet.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: View specific Event Log entry

    Still waiting desperately for a reply...
    Last edited by TheThinker; Dec 22nd, 2012 at 08:53 AM.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: View specific Event Log entry

    Problem solved thanks to a reply in another thread...
    Thanks anyways..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width