Friday, May 15, 2009

Write to System Log in Vb.net

To write an entry to System Log, you could do it 2 ways:
  1. Use existing source "Application" in the Event Viewer
  2. Create new source just for your application
BTW: To write logs, all functions are defined under System.Diagnositcs.EventLog namespace. Use: Imports System.Diagnostics.EventLog
To write to existing source, use:
EventLog.WriteEntry("TestApp", "This is Test") -- This will write log under Application with source name as TestApp.
To write to new source, use:
Dim objEventLog As New System.Diagnostics.EventLog()
Dim AppName As String = "Test App"
Dim LogName As String = "My Log"

'Register the App as an Event Source
If Not System.Diagnostics.EventLog.SourceExists(AppName) Then
       System.Diagnostics.EventLog.CreateEventSource(AppName, LogName)
End If
objEventLog.Source = LogName
objEventLog.WriteEntry("This is Test")

HTH.

No comments: