Skip to content

Instantly share code, notes, and snippets.

@TravisTheTechie
Created August 6, 2010 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravisTheTechie/510668 to your computer and use it in GitHub Desktop.
Save TravisTheTechie/510668 to your computer and use it in GitHub Desktop.
namespace StuffOnAShelf
{
using System;
using System.IO;
using System.Timers;
using log4net;
using log4net.Config;
using Topshelf.Configuration.Dsl;
using Topshelf.Shelving;
public class AShelvedClock :
Bootstrapper<TheClock>
{
public void InitializeHostedService(IServiceConfigurator<TheClock> cfg)
{
cfg.HowToBuildService(n => new TheClock());
cfg.WhenStarted(s =>
{
XmlConfigurator.Configure(new FileInfo(Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"clock.log4net.config")));
s.Start();
});
cfg.WhenStopped(s => s.Stop());
}
}
public class TheClock
{
readonly Timer _timer;
readonly ILog _log = LogManager.GetLogger(typeof(TheClock));
public TheClock()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment