Home All Groups Group Topic Archive Search About

Problem with EventLog

Author
8 Jun 2006 3:49 PM
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




--
Thanx in Advance,

atr2000

Author
9 Jun 2006 6:16 PM
Ravi Shankar
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.
--
Ravi Shankar


Show quoteHide quote
"John Smith" wrote:

> 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
>
>
>
>
> --
> Thanx in Advance,
>
> atr2000
>
>
>
Author
20 Jun 2006 1:38 PM
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....

--
Thanx,

atr2000
Show quoteHide quote
"Ravi Shankar" <shankycheil@newsgroup.nospam> wrote in message
news:7496F810-D3E7-4127-9099-AA0631EFEC69@microsoft.com...
> 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.
> --
> Ravi Shankar
>
>
> "John Smith" wrote:
>
> > 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
> >
> >
> >
> >
> > --
> > Thanx in Advance,
> >
> > atr2000
> >
> >
> >