Problem creating event log

G

Guest

I hope this is just something stupid I'm missing and someone can easily point
out my error. I'm trying to do a few turorials on windows services, and the
couple I found so far just create an event log and write an entry on start
and stop. I build the projects and install them fine, but when I run the
service it starts and stops instantly. The service is not creating the
custom event log I wanted it to but instead adds an error message in the
Application event log saying that the source I declared for my eventlog was
not registered to the eventlog I declaired. I'm not sure I understand
because I found several articles on line saying if you included a custom
source and log for an EventLog component, if they were not present they were
automatically created. Here is the code I use to intialize the event log:

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call
' Test if the event log exists; if not, create it
If (Not System.Diagnostics.EventLog.SourceExists("ServiceOne")) Then
EventLog.CreateEventSource("ServiceOne", "ServiceOneLog")
End If
' Set the source and log properties for the event log
EventLog1.Source = "ServiceOne"
EventLog1.Log = "ServiceOneLog"
End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog1.WriteEntry("ServiceOne is starting.")
End Sub

I'm not sure why this is failing. All the examples I find say this should
work. Do I need to set something up to be allowed to create an event log?
I've tried setting up the service as both a local service and as a service
under an admin user with now luck? Can anyone give me any advice on what I'm
missing?

Chris
 
N

niclarke

I hit the same wall and after some debugging found out that the service
did not have the correct security rights to create a event log.

You can change the security setting, but after some searching quite some
people recomemded creating the event log during the install (Custom Action).

This is the point that I'm at. If I get it to work 100% I will let you
know.

Here are some links I have used so far:

http://msdn.microsoft.com/library/d...temdiagnosticseventloginstallerclasstopic.asp

http://msdn.microsoft.com/library/d...bwlkWalkthroughCreatingEventLogInstallers.asp

Nick
 
N

niclarke

I hit the same wall but after some debugging found out that the service
did not have the correct security rights to create a event log.

You can change the security setting, but after some searching quite some
people recommended creating the event log during the install (Custom
Action).

This is the point that I'm at. If I get it to work 100% I will let you
know.

Here are some links I have used so far:

http://msdn.microsoft.com/library/d...temdiagnosticseventloginstallerclasstopic.asp

http://msdn.microsoft.com/library/d...bwlkWalkthroughCreatingEventLogInstallers.asp

Nick
 
K

Kris

Hi Chris,

Try impersonating the service to run under admin account. And I'm using
the below code, it works fine.

' creation of event log if it does not exist
Dim eventLogName As String = "PROS"
If Not EventLog.SourceExists(eventLogName) Then
EventLog.CreateEventSource(eventLogName, eventLogName)
End If

' insert into eventlog
Dim log As New EventLog()
log.Source = eventLogName
log.WriteEntry(errorDescription, EventLogEntryType.Error)

Cheers,
Kris
 

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