Search This Blog

Sunday, January 17, 2010

Creating And Manipulating Custom EventLog with Vb.net

Imports System.Diagnostics
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.Button2 = New System.Windows.Forms.Button()
Me.Label4 = New System.Windows.Forms.Label()
Me.TextBox3 = New System.Windows.Forms.TextBox()
Me.Button3 = New System.Windows.Forms.Button()
Me.Label5 = New System.Windows.Forms.Label()
Me.TextBox4 = New System.Windows.Forms.TextBox()
Me.Button4 = New System.Windows.Forms.Button()
Me.Button5 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(128, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(256, 56)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Create Source"
'
'ListBox1
'
Me.ListBox1.Items.AddRange(New Object() {"Information", "Error", "Warning"})
Me.ListBox1.Location = New System.Drawing.Point(128, 121)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120, 17)
Me.ListBox1.TabIndex = 2
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(128, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = ""
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(60, 14)
Me.Label1.TabIndex = 4
Me.Label1.Text = "LogName"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(16, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(45, 14)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Source"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(16, 113)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(63, 14)
Me.Label3.TabIndex = 6
Me.Label3.Text = "EntryType"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(256, 16)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 7
Me.Button2.Text = "Create Log"
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label4.Location = New System.Drawing.Point(16, 153)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(65, 14)
Me.Label4.TabIndex = 9
Me.Label4.Text = "Discription"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(128, 153)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.TabIndex = 8
Me.TextBox3.Text = ""
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(112, 184)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(96, 23)
Me.Button3.TabIndex = 10
Me.Button3.Text = "Write Entry"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.Location = New System.Drawing.Point(320, 96)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(60, 14)
Me.Label5.TabIndex = 12
Me.Label5.Text = "LogName"
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(432, 96)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.TabIndex = 11
Me.TextBox4.Text = ""
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(448, 136)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 13
Me.Button4.Text = "Clear Log"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(448, 168)
Me.Button5.Name = "Button5"
Me.Button5.TabIndex = 14
Me.Button5.Text = "Drop Log"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(608, 342)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button5, Me.Button4, Me.Label5, Me.TextBox4, Me.Button3, Me.Label4, Me.TextBox3, Me.Button2, Me.Label3, Me.Label2, Me.Label1, Me.TextBox2, Me.ListBox1, Me.Button1, Me.TextBox1})
Me.Name = "Form1"
Me.Text = "Creating and Manipulating Custom Log"
Me.ResumeLayout(False)

End Sub

#End Region
Dim NewEventLog As EventLog
Dim xeventEntryType As EventLogEntryType
Dim strLogName As String
Dim strSourceName As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

strLogName = TextBox1.Text
If EventLog.Exists(strLogName) Then
MessageBox.Show("Log Already exist")
Else
NewEventLog = New EventLog(strLogName)
MessageBox.Show("New Log created")
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

strSourceName = TextBox2.Text

If EventLog.SourceExists(strSourceName) Then
MessageBox.Show("Source Already exist")
Else
NewEventLog.CreateEventSource(strSourceName, strLogName)
MessageBox.Show("New Source created")
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim strEntryType As String
Dim strDesc As String
strEntryType = ListBox1.Text
strDesc = TextBox3.Text
Select Case strEntryType
Case "Information"
xeventEntryType = EventLogEntryType.Information
Case "Warning"
xeventEntryType = EventLogEntryType.Warning
Case "Error"
xeventEntryType = EventLogEntryType.Error
Case "FailureAudit"
xeventEntryType = EventLogEntryType.FailureAudit
Case "SuccessAudit"
xeventEntryType = EventLogEntryType.SuccessAudit
End Select
NewEventLog.Source = strSourceName
NewEventLog.WriteEntry(strDesc, xeventEntryType)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
strLogName = TextBox4.Text
If EventLog.Exists(strLogName) Then
NewEventLog = New EventLog(strLogName)
NewEventLog.Clear()
MessageBox.Show("ALL entries in Log Are deleted")
Else
MessageBox.Show("No such Log Exist")
End If
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
strLogName = TextBox4.Text
If EventLog.Exists(strLogName) Then
EventLog.Delete(strLogName)
MessageBox.Show("Log is dropped ")
Else
MessageBox.Show("No such Log Exist")
End If

End Sub
End Class

No comments:

Post a Comment