public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private string strELSource = "EventLogTheft";
private string strELLogName = "EventLogTestApp";
private string strELComputerName = ".";
private int iCounter = 0;
private Timer tmr5Seconds = null;
public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
try
{
System.Timers.Timer tmr5Seconds = new System.Timers.Timer(1000);
tmr5Seconds.Elapsed+= new ElapsedEventHandler(tmr5Seconds_Elapsed);
if(!EventLog.SourceExists(strELSource, strELComputerName))
{
Debugger.Break();
Console.WriteLine("Source does not exist. I'll try to create");
//EventLog.CreateEventSource(strELSource, strELLogName, strELComputerName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
if (EventLog.SourceExists(strELSource, strELComputerName))
{
tmr5Seconds.Enabled = true;
}
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
tmr5Seconds.Stop();
}
private void tmr5Seconds_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
iCounter+=1;
EventLog el1 = new EventLog();
el1.Source = strELSource;
el1.Log = strELLogName;
el1.WriteEntry("Timed event. Second: " + iCounter.ToString());
Console.WriteLine(iCounter.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}