Visual Basic.NET
  Home arrow Visual Basic.NET arrow Managing `EventLog` using Visual Basic.NET...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Managing `EventLog` using Visual Basic.NET and VBScript
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2006-01-18

    Table of Contents:
  • Managing `EventLog` using Visual Basic.NET and VBScript
  • How to list all “Blue Screen” events (or STOP errors) using Visual Basic.NET
  • How to clear “EventLog” dynamically using Visual Basic.NET
  • How to copy “EventLog” information into a text file using Visual Basic.NET

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Managing `EventLog` using Visual Basic.NET and VBScript
    (Page 1 of 4 )

    This article explains how to manage “EventLog” information dynamically using Visual Basic.NET and VBScript. You will learn how to list all events, how to make a backup of the "Eventlog" dynamically, and how to perform many other tasks as well.
    A downloadable file for this article is available here.

    The sample downloadable solution (zip) was entirely developed using Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003 Standard Edition.  But, I am confident that it would work with other versions of Windows (which support .NET 1.1) as well.

    I contributed several articles on WMI with VB.NET and VBScript (including the articles on introductory or basic topics of WMI).  I even contributed a series (of about six articles) on “WMI Programming on VB.NET” covering several aspects of WMI. I strongly suggest you go through the series, before going through this article.

    How to list all events from “EventLog” using Visual Basic.NET

    Before getting the information out of “EventLog”, we need to create a wrapper to store the EvenLog information. Let us proceed to create a wrapper:

    Public Function getEventLogStructure() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn("Category"))
            dt.Columns.Add(New DataColumn("ComputerName"))
            dt.Columns.Add(New DataColumn("EventCode"))
            dt.Columns.Add(New DataColumn("Message"))
            dt.Columns.Add(New DataColumn("TimeWritten"))
            dt.Columns.Add(New DataColumn("Type"))
     
            Return dt
        End Function

    The following method “addEventLog” adds a single row based on the structure you create for the data table using the above method.

        Public Sub addEventLog(ByRef dt As DataTable, ByVal Category
    As String, ByVal ComputerName As String, ByVal EventCode As
    String, ByVal Message As String, ByVal TimeWritten As String,
    ByVal Type As String)
            Dim dr As DataRow
            dr = dt.NewRow
            dr("Category") = Category
            dr("ComputerName") = ComputerName
            dr("EventCode") = EventCode
            dr("Message") = Message
            dr("TimeWritten") = TimeWritten
            dr("Type") = Type
            dt.Rows.Add(dr)
        End Sub

    Once you complete the creation of the wrapper, the following VB.NET code should support some minimum information about “SoundDevice” available on your system.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NTLogEvent")
                Dim dt As DataTable = globals.getEventLogStructure
                For Each queryObj As ManagementObject In searcher.Get
    ()
                    globals.addEventLog(dt, Convert.ToString(queryObj
    ("Category")), queryObj("ComputerName"), Convert.ToString
    (queryObj("EventCode")), queryObj("Message"), Convert.ToString
    (queryObj("TimeWritten")), Convert.ToString(queryObj("Type")))
                Next
                Me.DataGrid1.DataSource = dt
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for
    WMI data: " & err.Message)
            End Try
        End Sub

    You can achieve the same result with VBScript as follows:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_NTLogEvent",,48)
    For Each objItem in colItems
        Wscript.Echo "Category: " & objItem.Category
        Wscript.Echo "ComputerName: " & objItem.ComputerName
        Wscript.Echo "EventCode: " & objItem.EventCode
        Wscript.Echo "Message: " & objItem.Message
        Wscript.Echo "TimeWritten: " & objItem.TimeWritten
        Wscript.Echo "Type: " & objItem.Type
    Next

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello, Now you can manage event log dynamically. Just read and enjoy. You can...
     

    VISUAL BASIC.NET ARTICLES

    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic
    - Manipulating Forms with the Windows Forms Li...
    - Basics of the Windows Forms Library
    - Forms, Controls, and Other Useful Objects
    - Implementing OOP to Develop Database Oriente...
    - Using Themes and Skins for Personalization w...
    - A Deeper Look at Personalization using Visua...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway