Problem with EventLog

J

John Smith

I have created a windows service that logs errors to the EventLog. I want
the logs to go to an EventLog I have created. However, I am finding that
the event is not logging to the created EventLog instead it is logging to
Application log. What could be the problem?

========================================================================
SYSTEM DETAILS:
========================================================================

Operating System: Windows 2000 Professional SP4
..NET Version: 1.1.4322

========================================================================
CODE:
========================================================================

Public Class MyClass

Private _EventSourceName As String = "MySource"
Private _EventLogName As String = "MyLog"

'**********************************************************************
'*** DESCRIPTION: Adds entries to the Application Log.
'**********************************************************************
Private Sub Add( _
ByVal DisplayMessage As String, _
ByVal objEventLogEntryType As EventLogEntryType, _
ByVal EventID As Integer, _
ByVal Category As Short _
)

'*** Create new event log ***
Dim oEventLog As New EventLog

'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

'*** Set the event log source ***
oEventLog.Log = _EventLogName
oEventLog.Source = _EventSourceName

'*** Write to the event log ***
oEventLog.WriteEntry(_EventSourceName, DisplayMessage,
objEventLogEntryType, EventID, Category)

End Sub


'**********************************************************************
'*** DESCRIPTION: Creates "Variable Information" entry. This is
'*** mainly used for testing purposes.
'**********************************************************************
Public Sub Message( _
ByVal Value As String, _
Optional ByVal objEventLogEntryType As EventLogEntryType =
EventLogEntryType.Information, _
Optional ByVal EventID As Integer = 0, _
Optional ByVal Category As Short = 0 _
)

'*** Declare Variables ***
Dim strMessage As String

'*** Initialize Variables ***
strMessage = "Message" & vbCrLf & vbCrLf
strMessage &= Value & vbCrLf

'*** Add Application Log Entry ***
Add(strMessage, objEventLogEntryType, EventID, Category)

End Sub

End Class

'***************************************
'*** MY FUNCTION CALL ***
'***************************************
Dim oApplicationEntry As New MyClass
oApplicationEntry.Message("TEST", EventLogEntryType.Error, 9999)
oApplicationEntry = Nothing
 
G

Guest

Hi John,

The issue I see is with the statement
'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

The default project installer action creates the event source in the
"Application" log, So the "SourceExists" would always return true and the
"CreateEventSource" statement would never get executed. If this is indeed a
service then I'd recommend you use the Me.EventLog option
(ServiceBase.EventLog) to set the source & Log for logging messages.
 
J

John Smith

Ravi,

Thank you for the idea but unfortunately that did not help. However I did
figure it out. I changed the code to more like the following.

'*** Create new event log ***
Dim oEventLog As New EventLog

'*** Since I know that my log name is unique, I am not afraid to remove
the source registration from the Application log ***
If oEventLog.LogNameFromSourceName(_EventSourceName,
Environment.MachineName).ToLower <> _EventLogName.ToLower Then
'*** Delete Source ***
oEventLog.DeleteEventSource(_EventSourceName)
End If

'*** If the the Event Log does not exist, then create one. ***
If oEventLog.Exists(_EventLogName) = False Or
oEventLog.SourceExists(_EventSourceName) = False Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

..... Etc....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top