Visual Studio .NET 2003
Swigart Consulting LLC
April 2006
Applies to:
Visual Basic 6.0
.NET Framework
Summary: This article explains how to use .NET Framework classes to add rich event logging functionality to existing Visual Basic 6.0 applications. (6 printed pages)
Contents
IntroductionThe .NET Framework EventLog Class
Instrumentation
Reading the Logs
Conclusion
Click here to download the code sample for this article.
Introduction
Visual Basic 6.0 provides some ability to write to the event log through App.LogEvent, but this API has a number of serious limitations. First, you aren't able to define the source for your events (it always appears as VBRuntime in the event viewer), and you can't specify the event ID or category. You're also limited to writing to the Application event log, and you can't create your own custom sources and logs.By using the classes provided (for free) by the .NET Framework, you can overcome all of these limitations, and effectively instrument your applications so that you, administrators, or other support people can look at the logs to diagnose issues.
The .NET Framework EventLog Class
The .NET Framework provides a powerful EventLog class that makes it trivial to work with the event log. While you can't access this class directly from Visual Basic 6.0, in the spirit of the VB Fusion article series, I've made this functionality accessible from Visual Basic 6.0 by creating a COM wrapper around the .NET classes.I've also created a sample Visual Basic 6.0 application that exercises this event log functionality (see Figure 1).

Figure 1. Visual Basic 6.0 application that uses event log functionality
Most of the work in creating wrapper classes simply involves converting .NET types to types understood by Visual Basic 6.0. The GetEventLogs method normally returns an array of EventLog objects, and these EventLog
objects can't be used directly from Visual Basic 6.0. However, the
Visual Basic .NET function shown in Listing 1 returns a list of all the
event logs on the machine as an array of strings, which Visual Basic 6.0
can easily consume. Typically, you will have System, Security, and
Application event logs, but some applications also create their own
custom event logs, and this method will show you those as well.
Listing 1. Listing all the event logs on a machine using Visual Basic .NET
Public Function GetEventLogs() As String() Dim logs(EventLog.GetEventLogs.Length - 1) As String Dim i As Integer = 0 For Each el As EventLog In EventLog.GetEventLogs() logs(i) = el.Log i += 1 Next Return logs End Function
Calling this from Visual Basic 6.0 and, for example, populating a ListBox with the results is as simple as the code in Listing 2.
Listing 2. Displaying log names in a ListBox using Visual Basic 6.0
Dim eventLog As NetFrameworkWrappers.EventLogWrapper Set eventLog = New NetFrameworkWrappers.EventLogWrapper lstEventLogs.Clear Dim logs() As String logs = eventLog.GetEventLogs Dim log As Variant For Each log In logs lstEventLogs.AddItem log Next
Instrumentation
You may want your application to write to the event log for a variety of reasons. Certainly, any catastrophic errors should go into the event log, because this information can be used later to diagnose the problem. Quite often, you may want to write detailed information to the event log that would not be appropriate to show to the user.The first choice is which log to use. You can choose from the System, Security, or Application event logs. The Application event log is almost always the right choice. However, you can also create your own custom event log if you desire. Listing 3 shows the Visual Basic .NET code in the event log wrapper that lets you create your own custom log.
Listing 3. Creating a custom event log using Visual Basic .NET
Public Sub CreateEventSource(ByVal source As String, ByVal logName As String) If EventLog.SourceExists(source) Then EventLog.DeleteEventSource(source) End If EventLog.CreateEventSource(source, logName) End Sub
Listing 4. Creating an event log from Visual Basic 6.0 with the wrapper class
Dim eventLog As NetFrameworkWrappers.EventLogWrapper Set eventLog = New NetFrameworkWrappers.EventLogWrapper eventLog.CreateEventSource "MyApplication", "MyCustomLog"
Listing 5. Writing an entry to the event log in Visual Basic .NET
Public Sub WriteEntry(ByVal source As String, _ ByVal message As String, _ Optional ByVal type As String = "Information", _ Optional ByVal eventID As Integer = 0, _ Optional ByVal category As Short = 0) Dim typeEnum As EventLogEntryType = _ System.Enum.Parse(GetType(EventLogEntryType), type) m_eventLog.WriteEntry(source, message, typeEnum, eventID, category) End Sub
You can call this from Visual Basic 6.0 with the code in Listing 6.
Listing 6. Writing an event log entry from Visual Basic 6.0
Dim eventLog As NetFrameworkWrappers.EventLogWrapper Set eventLog = New NetFrameworkWrappers.EventLogWrapper eventLog.WriteEntry cboLog.Text, txtMessage, cboType.Text, _ txtID, txtCategory
Reading the Logs
Reading the information out of the event logs is only slightly more complex. The .NET Framework EventLog class has a GetEntries method that will return all the log entries for a give event log (Application, System, and so on). The problem is that each entry comes back as an EventLogEntry object, which can't be passed directly back to Visual Basic 6.0. However, it was simple for me to create my own EventLogEntryInfo class that I could copy each event log entry into. This is a full COM Class that can be returned to Visual Basic 6.0, and it exposes properties for the entry source, message, type, ID, and category. The Visual Basic .NET code then fills an array of these with the event log entry data, and returns it so that it can be used from Visual Basic 6.0 (see Listing 7).
Listing 7. Reading and converting event log entries from Visual Basic .NET
Public Sub GetEntries(ByRef entries() As EventLogEntryInfo) ReDim entries(m_eventLog.Entries.Count - 1) For i As Integer = 0 To m_eventLog.Entries.Count - 1 Dim ent As EventLogEntry = m_eventLog.Entries(i) entries(i) = New EventLogEntryInfo entries(i).Category = ent.CategoryNumber entries(i).Message = ent.Message entries(i).Source = ent.Source entries(i).EventID = ent.InstanceId entries(i).EntryType = ent.EntryType Next End Sub
Listing 8. Getting the Event Log entries from Visual Basic 6.0
Dim eventLog As NetFrameworkWrappers.EventLogWrapper Set eventLog = New NetFrameworkWrappers.EventLogWrapper eventLog.Init logName Dim entries() As NetFrameworkWrappers.EventLogEntryInfo eventLog.GetEntries entries
Tidak ada komentar:
Posting Komentar
Catatan: Hanya anggota dari blog ini yang dapat mengirim komentar.