Source: Christoph De Baene
Topshelf is an open-source hosting framework for building Windows Services using .NET. With Topshelf you can create in a few lines of code your own windows service. It’s a kind of internal DSL for building windows services. I used version 2.2 of Topshelf and the binaries and sources can be found here (GitHub).
First download Topshelf from GitHub, I used version 2.2 (direct link).
- Create a console application named ‘SampleWindowsService‘ inside Visual Studio
[message type="info"]Be sure to change the target framework to ‘.NET Framework 4‘[/message]
- Reference the binaries TopShelf.dll and log4net.dll (included in Topshelf).
- Create a simple service called ‘SampleService’ that simply write every 5 seconds to the log. Note that we create explicit a Start and Stop method which is conceptually the minimum that a windows service need.[csharp]
public class SampleService
private Timer _timer = null;
readonly ILog _log = LogManager.GetLogger(
double interval = 5000;
_timer = new Timer(interval);
_timer.Elapsed += new ElapsedEventHandler(OnTick);
protected virtual void OnTick(object sender, ElapsedEventArgs e)
_log.Debug("Tick:" + DateTime.Now.ToLongTimeString());
public void Start()
_log.Info("SampleService is Started");
_timer.AutoReset = true;
_timer.Enabled = true;
public void Stop()
_log.Info("SampleService is Stopped");
_timer.AutoReset = false;
_timer.Enabled = false;
- In the main method of our console application we will use Topshelf to host our SampleService.
We we are telling Topshelf how to start and stop the service, what the service name is, etc.
Note that we need to configure log4net for Topshelf and our service![csharp]
static void Main(string args)
var host = HostFactory.New(x =>
s.ConstructUsing(name => new SampleService());
s.WhenStopped(tc => tc.Stop());
- The only thing we have to do now is to configure log4net. Create a file called ‘log4net.config‘ with the following configuration.[xml]
<!–?xml version="1.0" encoding="utf-8" ?–>
This configuration enables to output to the console and through a UDP network protocol so that we can easily monitor the log statements when installed as a windows service. I used Log2Console (Codeplex) to monitor my log statements through UDP.
[message type="info"]Make sure the output directory of log4net.config is set to ‘Copy always’[/message] [message type="warning"]Note that there is an issue with log4net related to IPv6 and Windows Vista/7. You can fix it by adding the following ’127.0.0.1 log4view-local’ to your hosts file which can be found in folder C:WindowsSystem32driversetchosts.[/message]
When you fit F5 you will see that Topshelf outputs some log statements and you will see that the log statements of our SampleService is included and everything is working properly.
In order to install SampleService as a Windows Service you simply need to do the following through the command prompt.
[message type="info"]Be sure to launch the command prompt as an administrator![/message]
After that when the windows service has been installed successfully we can start the service through services.msc or simply by typing
Now we can open Log2Console to monitor our log files that is send through the UDP appender.
The sources can be found here (BitBucket)