Senin, 02 Maret 2015

CARA MEMBUAT KONEKSI EVENT LOG DI VB.NET



Menu membuat event log


Hi programmer
kali ini saya membagikan ilmu tentang event viewer / log dimana event ini adalah aplikasi bawaan dari windows nya yaitu membuat setaip kejadian-kejadian dan di catat atau di record di windowsnya, jadi yang saya terapkan ini hanya membuat logname baru, sourcename baru dan entry-entry massage baru..
berikut codingnya



CODING :

 Dim logName As String = "GramediaVending", sourceName As String = "Form Admin"
 Dim el As New EventLog

'membuat log baru
 Private Sub btnEventCreateLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEventCreateLog.Click
  If Not EventLog.Exists(logName) AndAlso Not EventLog.SourceExists(sourceName, logName) Then EventLog.CreateEventSource(sourceName, logName)
 End Sub

'delete log
 Private Sub btnEventDeleteLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEventDeleteLog.Click
  If EventLog.Exists(logName) Then EventLog.Delete(logName)
 End Sub

'delete source
 Private Sub btnEventDeleteSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEventDeleteSource.Click
  'If EventLog.Exists(logName) AndAlso EventLog.SourceExists(sourceName) Then EventLog.DeleteEventSource(sourceName)
 End Sub

'membuat entry message
 Private Sub btnEventWriteEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEventWriteEntry.Click
   el.Log = logName
  el.Source = sourceName
  el.WriteEntry("Testing", EventLogEntryType.Information)
 End Sub

'menampilkan log di eventviewer ke tabel datagrid
 Private Sub btnEventView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEventView.Click
  Try
   Dim searcher As New ManagementObjectSearcher("root\CIMV2", "Select * from Win32_NTLogEvent Where Logfile = 'GramediaVending'") '& " and SourceName = 'MySource2'")
   Dim dt As DataTable = getEventLogStructure()

   For Each queryObj As ManagementObject In searcher.Get()
    Dim d = queryObj("TimeWritten")

    d = Strings.Mid(d, 1, 4) & "/" & Strings.Mid(d, 5, 2) & "/" & Strings.Mid(d, 7, 2) & " " & Strings.Mid(d, 9, 2) & ":" & Strings.Mid(d, 11, 2) & ":" & Strings.Mid(d, 13, 2)
    Call addEventLog(dt, queryObj("Category"), queryObj("ComputerName"), queryObj("EventCode"), queryObj("Message"), d, queryObj("Type"))
   Next

   With Me.dgvEvent
    .DataSource = dt
    .AutoResizeColumns()
    dgvEvent.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    .DefaultCellStyle.WrapMode = DataGridViewTriState.True
   End With

  Catch ex As Exception
   MessageBox.Show("An error occurred while querying for WMI data: " & ex.Message)
  End Try
 End Sub

'delete log atau clear log
 Private Sub btnEventClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEventClear.Click
el.clear()
el.close()
 End Sub

 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

 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


HASIL DARI EVENT LOG


Cara masuk ke event log di window 7
Control panel - Administrative Tool - Event Viewer

Sekian
Selamat mencoba :)

Tidak ada komentar:

Posting Komentar