Visual Basic read Event Viewer "Application" log

Author Message
Snickasaurus

  • Total Posts : 8
  • Scores: 0
  • Reward points : 0
  • Joined: 6/26/2010
  • Status: offline
Visual Basic read Event Viewer "Application" log Wednesday, September 07, 2011 4:41 PM (permalink)
0
My apologies if I'm in the wrong place but hoping someone can shed some light on this.

As follows is the script I have pieced together. I take no credit except for changing two maybe three lines of code. My next coding adventure will be to adapt this into an HTA (my new found drug) and fancy it up a bit by adding radio buttons or perhaps even a drop down menu to choose a specific error, if possible. Also I've recently noticed there is an "EventID" number while working with this code so I, foolishly, assume this could be part of the drop down menu/radio button. In the code it says "Application" so I assume it means just the application logs. After multiple pages of Google searches I've given up as I think I'm just asking the wrong way. How can I alternate between System and Application logs from within an HTA?  Can anyone lend an e-hand? Thanks in advance for any help!

snickasaurus

 Public Class Form1 
 
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
 
 Dim eventLogApp As New System.Diagnostics.EventLog("Application") 
 
 Dim eventLogEntry As System.Diagnostics.EventLogEntry 
 
 Dim eventCntr As Integer = 1 
 
 For Each eventLogEntry In eventLogApp.Entries 
 
 Me.ListBox1.Items.Add("Event Number:" & eventCntr) 
 
 Me.ListBox1.Items.Add(eventLogEntry.EntryType.ToString) 
 
 Me.ListBox1.Items.Add(eventLogEntry.TimeGenerated.ToString) 
 
 Me.ListBox1.Items.Add(eventLogEntry.Source.ToString) 
 
 Me.ListBox1.Items.Add(eventLogEntry.Category.ToString) 
 
 Me.ListBox1.Items.Add(eventLogEntry.InstanceId.ToString) 
 
 Me.ListBox1.Items.Add(eventLogEntry.MachineName.ToString) 
 
 Me.ListBox1.Items.Add(eventLogEntry.Message.ToString) 
 
 Me.ListBox1.Items.Add("-----------------------------------------------") 
 
 eventCntr = eventCntr + 1 
 
 Me.ListBox1.Refresh() 
 
 Next 
 
 End Sub 
 
 End Class 

<message edited by Snickasaurus on Wednesday, September 07, 2011 5:01 PM>
 
#1
    Wakawaka

    • Total Posts : 456
    • Scores: 23
    • Reward points : 0
    • Joined: 8/27/2009
    • Status: offline
    Re:Visual Basic read Event Viewer "Application" log Thursday, September 08, 2011 1:56 AM (permalink)
    0
    You can use the following methods to get the general data you want.
     
     
    Sub GetLogEvents(computer, filter) 
        On Error Resume Next 
        
        Dim cEvents 
        Dim oEvent 
        Dim oWMI : Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2") 
         
        If filter = "" Then 
         Set cEvents = oWMI.ExecQuery("SELECT * FROM Win32_NTLogEvent", "WQL", &h10 + &h20) 
        Else 
         Set cEvents = oWMI.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE LogFile='" & filter & "'", "WQL", &h10 + &h20) 
        End If 
          
        For Each oEvent In cEvents 
         Call AddEvent("Computer Name: " & oEvent.ComputerName, oEvent.ComputerName) 
         Call AddEvent("Category: " & oEvent.Category, oEvent.Category) 
         Call AddEvent("Event Code: " & oEvent.EventCode, oEvent.EventCode) 
         Call AddEvent("Message: " & oEvent.Message, oEvent.Message) 
         Call AddEvent("Record Number: " & oEvent.RecordNumber, oEvent.RecordNumber) 
         Call AddEvent("Source: " & oEvent.SourceName, oEvent.SourceName) 
         Call AddEvent("Logged: " & oEvent.TimeWritten, oEvent.TimeWritten) 
         Call AddEvent("Type: " & oEvent.Type, oEvent.Type) 
         Call AddEvent("User: " & oEvent.User, oEvent.User) 
         Call AddEvent(String(15, "-"), "d01") 
        Next 
       End Sub 
        
       Sub AddEvent(text, value) 
        Dim oOption : Set oOption = Document.CreateElement("Option") 
         
        oOption.Text = text 
        oOption.Value = value 
         
        [Your HTML select element name].Add(oOption) 
       End Sub  
      

    I would recommend adding a date range to the event log queries as it can take a long time to populate the information you want.  VBScript is not as powerful as .Net as it is complied at run-time.
     
    As for how you are displaying the information, you might want to look at the div tag and adding the overflow css tag to it and then put a table within the div tag to make it scrollable in it's own area.
     
    To alter between them, you can make a select statement with "System" and "Application" options.  You can then pass this to the method provided to populate the log.  You can obviously build upon this base code as well.
    <message edited by Wakawaka on Thursday, September 08, 2011 2:06 AM>
     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9