Created
August 6, 2010 01:10
-
-
Save TravisTheTechie/510668 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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