Advertisement

Event Logger Windows Service in C# .NET A Step-By-Step Approach

In this article author explains how to develop your own event logger service with C#.NET

Windows Services are applications that run in the background and perform various tasks. At any moment, your Windows PC will likely have no less than twenty running services, handling everything from TCP/IP to task scheduling. Since Windows Services are generally “always on”, they provide the perfect platform for applications that require persistence but do not require user interaction. Services often come with simple applications that allow you to stop, start, or pause them. Most servers, such as IIS, SQL Server, and Apache run as services. Windows Services don’t run on Windows 95, 98, or ME; the NT kernel is a requirement. Let’s start developing our own simple event logger service with C#.NET.

STEP 1

  1. Open New Project from Visual Studio
  2. Select Project Types - > Windows
  3. Select Console Application from Visual Studio Installed Templates
  4. Change the Name, Location, Solution Name to some meaningful names(Say MyEventLog) and Select Create Directory for Solution

STEP 2

  1. Right Click on References in Solution Explorer and add two references System.Configuration.Install and System.ServiceProcess

STEP 3:

  1. Add using System.ServiceProcess;
  2. A Service class must inherit ServiceBase Class

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;

namespace MyEventLog
{
class Program:ServiceBase
{
static void Main(string[] args)
{
}
}
}

STEP 4:

  1. Switch to Design Mode of Program.cs
  2. Change the ServiceName property to MyLogEvent
  3. Select the ToolBox
  4. Drag the EventLog Tool from the ToolBox and drop to the form
  5. Switch to Code View of Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;

namespace MyEventLog
{
class Program:ServiceBase
{
private System.Diagnostics.EventLog eventLog1;

        static void Main(string[] args)
{
}
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// Program
//
this.ServiceName = "MyLogEvent";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

STEP 5

  1. Define the Constructor Program() as follows-

public Program()
{
InitializeComponent();
this.AutoLog = false;
if (!System.Diagnostics.EventLog.SourceExists("MyEventLogSource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MyEventLogSource", "MyNewEventLog");
}
eventLog1.Source = "MyEventLogSource";
eventLog1.Log = "MyNewEventLog";
}

 

STEP 6

  1. Create an object of your service class in Main-

static void Main(string[] args)
{
ServiceBase.Run(new Program());
}

 

STEP 7

  1. Override Start, Stop, Continue and Pause methods used for respective service

static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
protected override void OnStart(string[] arg)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}

STEP 8

  1. Switch to Design Mode of Program.cs

STEP 9

  1. Add Installer for your service by clicking on Add Installer option below at property window. 
  2. Look at Solution Explorer you will find that ProjectInstaller.cs and serviceProcessInstaller1 and serviceInstaller1 objects are added. 
  3. Click on serviceProcessInstaller1 object and change it’s Account property to LocalSystem
  4. Click on serviceInstaller1 object and change it’s StartType property to Manual

STEP 10

  1. Build the solution.

STEP 11

  1. Install the created service
  2. Go to Start - > Program File -> Microsoft Visual Studio 2005 ->Visual Studio Tools -> Right Click on Visual Studio 2005 Command Prompt -> Select Run As Administrator
  3. Switch to the directory where MyEventLog.exe file is stored from the command prompt.
  4. Use the command installutil /i MyEventLog.exe to install the service and installutil /u MyEventLog.exe to uninstall the service at the command prompt like this- C:\Users\Ajay\Documents\VisualStudio 2005\Projects\MyEventLog\MyEventLog\bin\Debug>installutil /i MyEventLog.exe

STEP 12

  1. If everything was right you will get two messages at the end The Commit phase completed Successful and Transacted install has completed.

STEP 13

  1. Now run the Service
  2. Go to Start Menu -> Programs -> Administrative Tools -> Services
  3. Scroll down to MyLogEvent service in Services Windows
  4. Right Click on MyLogEvent and Select Start.
  5. Now your custom service is ready and run in background.

STEP 14

  1. Find your Service
  2. Right Click on Computer and Select Manage
  3. Double Click on Event Viewer
  4. Scroll down to Log Summary and you will find your service is working.

About Author

Ajay Tripathi is working as Assistant professor at Jaipuria Institute of Management, Ghaziabad.

You may be contacted at ajayinvns@gmail.com








Added on April 30, 2012 Comment

Comments

#1

Sunitha commented, on April 30, 2012 at 3:35 p.m.:

Can anyone tell me how to display blog archive using asp.net with C#. Please give the url.

#2

Shekhar V commented, on April 30, 2012 at 3:42 p.m.:

No more running "eventlogger" services in W2k3/2k8 server? IS it True?

#3

saleem commented, on May 17, 2012 at 7:01 p.m.:

i am the student of mca please send me the code of the event logger project i need of this project.

Post a comment