Visual Basic.NET
  Home arrow Visual Basic.NET arrow Managing `EventLog` using Visual Basic.NET...
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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
     
     
    ADVERTISEMENT


    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...
       · i am not getting it, how to generate the log. i can only see the datagrid. can u plz...
     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT