• We just launched and are currently in beta. Join us as we build and grow the community.

How to Write an Entry to the Application Event Logs in Visual Basic 2008

BIGnigga

Crypto Analyst
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
109
Likes
93
Bits
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.

firstformeventlogs.png


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”.

  1. 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.

  1. Private

    Sub

    rdo_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    _
  2. Handles

    rdoWarning.

    Click

    , rdoInfo.

    Click

    , rdoError.

    Click

  3. 'THIS PROCEDURE HANDLES THE CLICK EVENT OF ALL THE RADIO BUTTONS.
  4. 'YOU WILL KNOW WHICH RADIO BUTTON WAS CLICKED BECAUSE
  5. ' IT WILL PASS IN AS THE "SENDER" ARGUMENT.
  6. Dim

    rdo As

    RadioButton =

    CType

    (

    sender, RadioButton)
  7. 'SET THE PROPERTY NAME OF THE RADIO BUTTONS
  8. Select

    Case

    rdo.

    Name
  9. Case

    "rdoWarning"

    'NAME OF THE RADIO BUTTON
  10. entry_type =

    EventLogEntryType.

    Warning
  11. Case

    "rdoInfo"

    'NAME OF THE RADIO BUTTON
  12. entry_type =

    EventLogEntryType.

    Information
  13. Case

    "rdoError"

    'NAME OF THE RADIO BUTTON
  14. entry_type =

    EventLogEntryType.

    Error
  15. End

    Select

  16. End

    Sub

Lastly, do this following code for writing an entry for your event logs.

  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. Try
  3. If

    IsNumeric

    (

    txtId.

    Text

    )

    Then
  4. 'WRITE THE NAME OF THE LOG ON THE FIRST PARAMETERS.
  5. 'FOR THE SECOND PARAMETER, IS THE NAME OF THE LOCAL MACHINE.
  6. 'AND FOR THE THIRD PARAMETER, IS THE SOURCE OF THE EVENT.
  7. 'THIS IS SET COMMONLY EQUAL TO THE NAME OF THE APPLICATION.
  8. Dim

    eventLog As

    New

    EventLog(

    "Application"

    , My.

    Computer

    .

    Name

    , "Write Event Logs"

    )

  9. 'SET THE FIRST PARAMETER FOR THE MESSAGE THAT YOU PUT IN THE TEXT BOX.
  10. 'FOR THE SECOND PARAMETER, IS THE ENTRY TYPE YOU WANT TO SET WHETHER
  11. ' IT IS A WARNING , AN IMFORMATION OR AN ERROR.
  12. 'AND THE THIRD PARAMETER, IS FOR THE ID OF THE EVENT.
  13. ' YOU CAN USE THIS TO LOOK FOR FURTHER INFORMATION IN THE HELP FILE
  14. eventLog.

    WriteEntry

    (

    txtname.

    Text

    , entry_type, CInt

    (

    txtId.

    Text

    )

    )

  15. eventLog.

    Close

    (

    )

  16. MsgBox

    (

    "Entry written to the event log."

    , MsgBoxStyle.

    OkOnly

    , Me

    .

    Text

    )
  17. Else
  18. 'THIS WILL PERFORM IF THE EVENT ID WAS NOT NUMERIC.
  19. MsgBox

    (

    "Put the numeric value into EventID text box."

    , MsgBoxStyle.

    OkOnly

    , _
  20. Me

    .

    Text

    &

    " Error"

    )
  21. End

    If
  22. Catch

    secEx As

    System.

    Security

    .

    SecurityException
  23. MsgBox

    (

    "Security error writing to the event log."

    , _
  24. MsgBoxStyle.

    OkOnly

    , Me

    .

    Text

    &

    " Error"

    )
  25. Catch

    ex As

    Exception
  26. MsgBox

    (

    "Error accessing logs on the local machine."

    , MsgBoxStyle.

    OkOnly

    , Me

    .

    Text

    &

    " Error"

    )
  27. End

    Try
  28. End

    Sub

 

452,292

323,348

323,357

Top