Windows Service - OnStart - Errorhandling

  • Thread starter Thread starter Erik Tamminga
  • Start date Start date
E

Erik Tamminga

Hi,

This has probably been asked before but ...

I've written a Windows Service in C#. Everything works fine, exept for the
fact that I do not want the service to start succesfull if it has problems
reading it's own configuration file. I've tried raising an execption, which
does infact cause the service not to start, but leaves a very dirty message
on the eventlog. I've also tried shutting down the service (via a
servicecontroller within the service) when a failure is detected, but this
adds a message "Service Started" and "Service Stopped" to the eventlog,
which looks like the service started succesfully to the user that started
it.

What's general practice in this issue?

Erik
 
You could write your own message to the eventlog. You can do this by using
the EventLog class (http://tinyurl.com/2tn46):
EventLog lets you access or customize Windows 2000 event logs, which record
information about important software or hardware events. Using EventLog, you
can read from existing logs, write entries to logs, create or delete event
sources, delete logs, and respond to log entries. You can also create new
logs when creating an event source.

If the log that you specify in a call to CreateEventSource does not exist on
the computer, the system creates a custom log and registers your application
as a source for that log. Use the EventLog class to read and write entries
to any event log for which you have the appropriate access.

Note The Security log is read-only.
If you write to an event log, you must specify or create an event Source.
The Source registers your application with the event log as a valid source
of entries. You can only use the Source to write to one log at a time. The
Source can be any random string, but the name must be distinct from other
sources on the computer. It is common for the source to be the name of the
application or another identifying string. An attempt to create a duplicated
Source value throws an exception. However, a single event log can be
associated with multiple sources.

To read from a log, specify the Log name and MachineName (server computer
name) for the EventLog. It is not necessary to specify the Source, as a
source is required only for writing to logs. The Entries member is
automatically populated with the event log's list of entries.

Note You are not required to specify the MachineName if you are
connecting to a log by specifying a Log/ MachineName pair. If you do not
specify the MachineName, the local computer, ".", is assumed.
When writing to an event log, you can specify the type of information sent
with the message parameter. In addition to sending the message, you can send
an EventLogEntryType to indicate whether the message is an error, warning,
or information entry type. You can also specify application-defined eventId
and category parameters to display in the Type and Category columns of the
event viewer. Finally, you can also attach binary data to your event entry
if you need to associate additional information with a given event.

In addition to accessing individual event logs and their entries, the
EventLog class provides access to the collection of all event logs. You can
use the static (Shared in Visual Basic) members of EventLog to delete logs,
get log lists, create or delete a source, or determine if a computer already
contains a particular source.

Windows 2000 has three default logs: Application, System, and Security.
Other installed applications and services, such as Active Directory, can
have additional event logs. You can use EventLog to create custom event logs
you can view through the server's Event Viewer.

Event logging consumes disk space, processor time, and other system
resources. It is important to log only essential information. It is
recommended that you place event log calls in an error path, rather than in
the main code path, so as not to adversely affect performance.

For a list of initial property values for an instance of EventLog, see the
EventLog constructor.
 
Back
Top