Thursday, January 24, 2008

Scheduling and Event Logging Windows Service

In previous post we had seen how to create a windows service. In this part we will see how can we log events in windows service and how to schedule service to occur at specific interval.



First we will see how we can Log events in EventViewer event of Windows Service. Steps are as follows.

- Add an EventLog to your windows service class, from Toolbox.

- Give an ID say "MyEventLog1", Set Log to "MyEventLog" and Source to "MyEventLogSource"



This will help you to differentiate your log in event viewer. And you can also create a new view of your event source log. Create new Log View on application and rename it to "MyEventLogSource". Now clear logs, then each new entry with log source "MyEventLogSource" will be logged in the new view.



Now how will we write a new log entry in Windows Service.

Write the following code in OnStart Code block of your Service.



MyEventLog1.WriteEntry("My Windows Service Started.......");



This will be write a log entry. But make sure that you write this code in try-catch block, because on some OS, Windows Service do not have permission to log events. At that time your service will generate an unhandeled exception and it will stop immediately and you will unable to identify the problem that why your service is stopped immediately after it starts.





There can be two types of windows service.

1) Windows service that run only once. There is no any scheduling of the service. If you want to run it again the you must start the service again. It will stop automatically after it complete its job.



2) Windows service that run at specific interval. This service is useful to run Insert command from one server to other backup server. Or it can be use to complete tasks that are added in Database.



The first type of Windows service is very easy. Just put your code in OnStart event of the Windows Service.


Scheduling Windows Service with Timer Control


For scheduling a Windows service declare two global variables in your service class (Make sure not in Service.designer.cs Designer class)
This timer control is not System.Timer, this control is
System.Threading.Timer



private Timer stateTimer;

private TimerCallback timerDelegate;



Timer is useful for creating the scheduling the Service. TimerCallback is useful for specifying the function which will be called when timer interval ends(Tick event fires).



Now write this code in your OnStart block.

timerDelegate = new TimerCallback(DoSomething);
stateTimer = new Timer(timerDelegate, null, 0, 5000); // 5000 = 5 secs

In above code the timerDelegate is a callback function which will specify the Function that will be called each time your timer fires tick event.

Now the stateTimer is assigned to a new timer object which specify timerDelegate, initial delay and interval. In above example '0' is initial delay which specify after timer object is created how much to delay for firing first Tick event. '5000' this specify the interval, this indicate the delay of firing the tick event since the last tick event is fired. Both Initial delay and Interval are specified in milisecond.


Now we will see how to create the function.

protected void DoSomething(object sender)
{
MyEventLog1.WriteEntry("Windows Service event fired at " + DateTime.Now.ToString());
}

Above I have specified the function to be fired at tick event of timer. It will log in EventViewer with date and time.


In next part we will see how can we add a configuration file to the Windows Service, how to install the service and common problem while using with Windows Server 2003 system.