Page 5 of 5
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:
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(
SysLog = new EventLog("System");
SysLog.EnableRaisingEvents = true;
The OnStop method now has to remove both event handlers from each of the log objects:
protected override void OnStop()
SysLog.EnableRaisingEvents = false;
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:
and add to the start of the OnStart method:
if (args.Length == 4)
SMTP = args;
email = args;
user = args;
password = args;
These values are used to set the address of the recipient:
MailMessage mail =new MailMessage(
and the details of the SMTP server:
SmtpClient mailbox =
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:
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.
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.