A Windows Service without a template
Written by Harry Fairhead   
Friday, 14 August 2009
Article Index
A Windows Service without a template
A simple service
Using Timers
Event log service
Other logs

Other logs

Most of the important events relating to the health of a PC are reported in the System log so it’s worth exploring how to extend Eventer’s monitoring to the System and so other logs. The only real difference is that to access another log we need to explicitly create an EventLog object:

EventLog SysLog;

OnStart now creates the EventLog object and sets its EntryWritten event handler to be the same as the default Application log:

protected override void OnStart(
string[] args)
SysLog = new EventLog("System");
SysLog.EntryWritten +=
new EntryWrittenEventHandler(
this.EventLog.EntryWritten +=
new EntryWrittenEventHandler(
= true;
SysLog.EnableRaisingEvents = true;

The OnStop method now has to remove both event handlers from each of the log objects:

protected override void OnStop()
= false;
this.EventLog.EntryWritten -=
new EntryWrittenEventHandler(
SysLog.EnableRaisingEvents = false;
SysLog.EntryWritten -=
new EntryWrittenEventHandler(

Now if you start the service you will receive emails for each new entry in the Application and in the System log.

A better design for a more robust service would start a new thread to send each email, thereby making sure that no events are missed, but this works well enough for most purposes.

Customising a service

Rather than hard coding information for the user name, SMTP server and so on into a service it would be much better to provide some customisable command line parameters. You might have noticed the args parameter in the OnStart method and might well be wondering if this can be used to pass configuration information. It can but it’s not quite as simple as it first appears.

To see it in action add the following variables:

String SMTP;
String email;
String user;
String password;

and add to the start of the OnStart method:

if (args.Length == 4)
SMTP = args[0];
email = args[1];
user = args[2];
password = args[3];

These values are used to set the address of the recipient:

MailMessage mail =new MailMessage(
"Eventer@myPC", email);

and the details of the SMTP server:

SmtpClient mailbox = 
new SmtpClient(SMTP);
mailbox.Credentials =
new NetworkCredential(user, password);

Now you can specify where you want the emails sent as a command line within the Service tool. All you have to do is open the Services properties and enter the parameters, separated by spaces, you want to pass to the service when you start it in the “Start Parameters” box.



Setting the start parameters


This is all very easy but the surprise is that the parameters you enter aren’t kept or used the next time you start the service. They are used once and then thrown away!

If you want to supply fixed configuration parameters then you have to enter them in the registry under the key:

<service name>

You can retrieve these fixed parameters using:


Not difficult but not particularly obvious!

Clearly services are easy to create useful but there is a lot more work needed to allow the user to configure and generally interact with a service.

Further reading

It is difficult to see why you would want a book dedicated to creating Windows services because once you have the basics then its just and application of general programming methods. This article plus a general book on .NET or a one targeted at the topic your service is dealing with is probably all you need. If you do feel the need of an accompanying book then try any of the three listed in the sidebar above - they all cover the same ground. Also see Programming Windows Services with Microsoft Visual Basic 2008.

If you would like the code for this project then register and click on CodeBin.










Last Updated ( Friday, 14 August 2009 )