Windows Service "Service could not be started" from EventLog call

144 views Asked by At

I'm trying to write an event log when the windows service is started. I'm using the property of the service, EventLog, which writes to the application log by default, like:

EventLog.WriteEntry("Service is starting up", EventLogEntryType.Information)

When I start the service using the net start command, it gives me

Service could not be started

error. When I remove these lines, it works just fine. Using the default:

System.Diagnostics.EventLog.WriteEntry("Application","Service is starting up", EventLogEntryType.Information)

works just fine. Why is the default not working?

1

There are 1 answers

7
John Koerner On

There is no static WriteEntry method on the EventLog class as defined in your call (one that takes a string and a EventLogEntryType).

There is an instance method with that signature and its documentation states:

You must set the Source property on your EventLog component before you can write entries to the log. You must create and configure the event source before writing the first entry with the source.

So, I assume you are using the instance method and you are failing to set the source.