Public Class Form1
Dim EventLog As New System.Diagnostics.EventLog()
Enum sLog As Integer
Application = 0 'Applicazione
System = 1 'Sistema
Security = 2
End Enum
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
InserisciEventoDefault()
End Sub
Private Sub MetodoEvntsLogs(ByVal sSource As String, ByVal sLogs As sLog, ByVal sEvent As String, ByVal sCodeErrorApplication As Integer, ByVal EventLogType As EventLogEntryType)
If sSource = Nothing Then
Exit Sub
End If
'sSource = "dotNET Sample App"
'sLog = "Application"
'sEvent = "Sample Event"
'sCodeErrorApplication = 233 'ID Event: 233 è il codice che si da l'applicazione
Dim sLog As String
Select Case sLogs
Case Is = 0
sLog = "Application"
Case Is = 1
sLog = "System"
Case Is = 2
sLog = "Security"
Case Else
sLog = "Application"
End Select
If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If
EventLog.WriteEntry(sSource, sEvent, EventLogType, sCodeErrorApplication)
End Sub
Private Sub MetodoEvntsLog(ByVal sSource As String, ByVal sLog As String, ByVal sEvent As String, ByVal sCodeErrorApplication As Integer, ByVal EventLogType As EventLogEntryType)
If sSource = Nothing And sLog = Nothing Then
Return
End If
If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If
EventLog.WriteEntry(sSource, sEvent, EventLogType, sCodeErrorApplication)
End Sub
Private Sub EliminaEventoLog(ByVal sSource As String, ByVal sLog As String)
Dim logName As String
If EventLog.SourceExists(sSource) Then
' Cerco il log associato con il Sorgente .
logName = EventLog.LogNameFromSourceName(sSource, ".")
' Se il lodName corissponde a quello che ho dichiarato procede alla eliminazione
'Altrimenti esci
If (logName <> sLog) Then
Return
End If
'Cancella il Sorgente e il Log
EventLog.DeleteEventSource(sSource)
EventLog.Delete(logName)
End If
End Sub
Private Sub InserisciEventoDefault()
Dim ssource As String = "MyApplication"
Dim slog As String = "Application"
If ssource = Nothing And slog = Nothing Then
Return
End If
If Not EventLog.SourceExists(ssource) Then
EventLog.CreateEventSource(ssource, slog)
End If
EventLog.WriteEntry(ssource, "Ciao Prova inserimento messaggio EventLog", EventLogEntryType.Information, 1000)
End Sub
End Class