Option Explicit On Option Strict Off Option Compare Text '============================================================================================================= ' ' modEventLog.vb ' -------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : July 23, 2003 ' Created On : July 23, 2003 ' ' VB Versions : VB.NET 1.1 (VS.NET 2003) ' ' Requires : The .NET Framework v1.1 ' ' Description : This class module makes it easy to log an event to the Window's Event Log of the local ' computer, or a remote computer on a network. These logged events can then later be viewed ' via the user of the Windows "Event Viewer" that comes pre-installed with Windows. ' ' Example Use : ' ' Dim intErrNum As Integer ' Dim strErrSrc As String ' Dim strErrDesc As String ' If cEventLog.LogEvent("This is a test log!", "Test Log", , , cEventLog.LogTypes.Application, EventLogEntryType.Information, intErrNum, strErrSrc, strErrDesc) = False Then ' MsgBox("The following error occured while trying to log the event:" & Chr(13) & Chr(13) & "Error Number = " & intErrNum.ToString & Chr(13) & "Error Source = " & strErrSrc & Chr(13) & "Error Description = " & strErrDesc, MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, " Error") ' Else ' MsgBox("Successfully logged event!", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, " ") ' End If ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Imports System.Diagnostics Public Class cEventLog #Region "Public Enumerations" Public Enum LogTypes Application = 0 System = 1 Securty = 2 Custom = 3 End Enum #End Region #Region "Public Functions" Public Shared Function LogEvent(ByVal sText As String, _ ByVal sEntryName As String, _ Optional ByVal sComputerName As String = ".", _ Optional ByVal iEventID As Integer = 0, _ Optional ByVal LogType As LogTypes = LogTypes.Application, _ Optional ByVal EntryType As EventLogEntryType = EventLogEntryType.Information, _ Optional ByRef Return_ErrNum As Integer = 0, _ Optional ByRef Return_ErrSrc As String = "", _ Optional ByRef Return_ErrDesc As String = "") As Boolean On Error GoTo ErrorTrap Dim oEventLog As EventLog Dim strLogName As String '// Set default values Return_ErrNum = 0 Return_ErrSrc = "" Return_ErrDesc = "" '// Get the log type Select Case LogType Case LogTypes.Application : strLogName = "Application" Case LogTypes.System : strLogName = "System" Case LogTypes.Securty : strLogName = "Security" Case Else : strLogName = "Custom" End Select '// Make sure the event source exists If EventLog.SourceExists(sEntryName, sComputerName) = False Then EventLog.CreateEventSource(sEntryName, strLogName, sComputerName) End If '// Write the event in the appropriate log oEventLog = New EventLog(strLogName, sComputerName, sEntryName) oEventLog.WriteEntry(sText, EntryType, iEventID) '// Clean up oEventLog = Nothing Return True ErrorTrap: Return_ErrNum = Err.Number Return_ErrSrc = Err.Source Return_ErrDesc = Err.Description Err.Clear() End Function #End Region End Class