|
Advertisement |
Event Logger Windows Service in C# .NET A Step-By-Step Approach
Posted On April 30, 2012 by Sneha Latha filed under
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
- Open New Project from Visual Studio
- Select Project Types - > Windows
- Select Console Application from Visual Studio Installed Templates
- Change the Name, Location, Solution Name to some meaningful names(Say MyEventLog) and Select Create Directory for Solution
STEP 2
- Right Click on References in Solution Explorer and add two references System.Configuration.Install and System.ServiceProcess
STEP 3:
- Add using System.ServiceProcess;
- 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:
- Switch to Design Mode of Program.cs
- Change the ServiceName property to MyLogEvent
- Select the ToolBox
- Drag the EventLog Tool from the ToolBox and drop to the form
- 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
- 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
- Create an object of your service class in Main-
static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
STEP 7
- 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
- Switch to Design Mode of Program.cs
STEP 9
- Add Installer for your service by clicking on Add Installer option below at property window.
- Look at Solution Explorer you will find that ProjectInstaller.cs and serviceProcessInstaller1 and serviceInstaller1 objects are added.
- Click on serviceProcessInstaller1 object and change it’s Account property to LocalSystem
- Click on serviceInstaller1 object and change it’s StartType property to Manual
STEP 10
- Build the solution.
STEP 11
- Install the created service
- Go to Start - > Program File -> Microsoft Visual Studio 2005 ->Visual Studio Tools -> Right Click on Visual Studio 2005 Command Prompt -> Select Run As Administrator
- Switch to the directory where MyEventLog.exe file is stored from the command prompt.
- 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
- If everything was right you will get two messages at the end The Commit phase completed Successful and Transacted install has completed.
STEP 13
- Now run the Service
- Go to Start Menu -> Programs -> Administrative Tools -> Services
- Scroll down to MyLogEvent service in Services Windows
- Right Click on MyLogEvent and Select Start.
- Now your custom service is ready and run in background.
STEP 14
- Find your Service
- Right Click on Computer and Select Manage
- Double Click on Event Viewer
- 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
Comments
Shekhar V commented, on April 30, 2012 at 3:42 p.m.:
No more running "eventlogger" services in W2k3/2k8 server? IS it True?
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.





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.