BIGnigga
Crypto Analyst
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial, I will teach you how to Write an Entry to the Application Event Logs by using Visual Basic 2008. Logs are very important because it will track every event that are happening in your application.
So, let’s begin:
1. Open Visual Basic 2008.
2. Create a new Windows Application.
3. Make the Form just like this.
Now, go to the code view. Create a variable that will set the entry type of the event log. And then, set it to default type which is “information”.
After that, you have to create a method for the entry type that you're going to use. Combine the three Radio Button that handles the click
events.
Lastly, do this following code for writing an entry for your event logs.
So, let’s begin:
1. Open Visual Basic 2008.
2. Create a new Windows Application.
3. Make the Form just like this.

Now, go to the code view. Create a variable that will set the entry type of the event log. And then, set it to default type which is “information”.
- Private
entry_type As
System.
Diagnostics
.
EventLogEntryType
=
EventLogEntryType.
Information
After that, you have to create a method for the entry type that you're going to use. Combine the three Radio Button that handles the click
events.
- Private
Sub
rdo_Click(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
_
- Handles
rdoWarning.
Click
, rdoInfo.
Click
, rdoError.
Click
- 'THIS PROCEDURE HANDLES THE CLICK EVENT OF ALL THE RADIO BUTTONS.
- 'YOU WILL KNOW WHICH RADIO BUTTON WAS CLICKED BECAUSE
- ' IT WILL PASS IN AS THE "SENDER" ARGUMENT.
- Dim
rdo As
RadioButton =
CType
(
sender, RadioButton)
- 'SET THE PROPERTY NAME OF THE RADIO BUTTONS
- Select
Case
rdo.
Name
- Case
"rdoWarning"
'NAME OF THE RADIO BUTTON
- entry_type =
EventLogEntryType.
Warning
- Case
"rdoInfo"
'NAME OF THE RADIO BUTTON
- entry_type =
EventLogEntryType.
Information
- Case
"rdoError"
'NAME OF THE RADIO BUTTON
- entry_type =
EventLogEntryType.
Error
- End
Select
- End
Sub
Lastly, do this following code for writing an entry for your event logs.
- Private
Sub
Button1_Click(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
Button1.
Click
- Try
- If
IsNumeric
(
txtId.
Text
)
Then
- 'WRITE THE NAME OF THE LOG ON THE FIRST PARAMETERS.
- 'FOR THE SECOND PARAMETER, IS THE NAME OF THE LOCAL MACHINE.
- 'AND FOR THE THIRD PARAMETER, IS THE SOURCE OF THE EVENT.
- 'THIS IS SET COMMONLY EQUAL TO THE NAME OF THE APPLICATION.
- Dim
eventLog As
New
EventLog(
"Application"
, My.
Computer
.
Name
, "Write Event Logs"
)
- 'SET THE FIRST PARAMETER FOR THE MESSAGE THAT YOU PUT IN THE TEXT BOX.
- 'FOR THE SECOND PARAMETER, IS THE ENTRY TYPE YOU WANT TO SET WHETHER
- ' IT IS A WARNING , AN IMFORMATION OR AN ERROR.
- 'AND THE THIRD PARAMETER, IS FOR THE ID OF THE EVENT.
- ' YOU CAN USE THIS TO LOOK FOR FURTHER INFORMATION IN THE HELP FILE
- eventLog.
WriteEntry
(
txtname.
Text
, entry_type, CInt
(
txtId.
Text
)
)
- eventLog.
Close
(
)
- MsgBox
(
"Entry written to the event log."
, MsgBoxStyle.
OkOnly
, Me
.
Text
)
- Else
- 'THIS WILL PERFORM IF THE EVENT ID WAS NOT NUMERIC.
- MsgBox
(
"Put the numeric value into EventID text box."
, MsgBoxStyle.
OkOnly
, _
- Me
.
Text
&
" Error"
)
- End
If
- Catch
secEx As
System.
Security
.
SecurityException
- MsgBox
(
"Security error writing to the event log."
, _
- MsgBoxStyle.
OkOnly
, Me
.
Text
&
" Error"
)
- Catch
ex As
Exception
- MsgBox
(
"Error accessing logs on the local machine."
, MsgBoxStyle.
OkOnly
, Me
.
Text
&
" Error"
)
- End
Try
- End
Sub