Windows-Service-Plus is a Visual Studio 2015 Project Template to get you started with a full featured Windows Service based on TopShelf and pre-loaded with logging, IoC container and more.
The idea is that you should be able to focus directly on your service logic right from the start, bypassing all the plumbing and boilerplate code.
Defaults are reasonable and will apply to most services but of course you can always change it to fit your needs
Since the project is based on TopShelf you get all the goodies the framework has to offer such as a service that is easier to test and deploy. Not to mention the hability to run and debug the service as a console application.
You don't have to be know TopShelf to take advantage of this project template but it is definitelly something worth checking.
Get the Visual Studio Extension: Visual Studio Gallery
After the project is created just hit F5
and it should download the required NuGet packages and run as a console application.
Your service logic should go on the Start
method of Service\WinService.cs
public bool Start(HostControl hostControl)
{
Log.Info($"{nameof(Service.WinService)} Start command received.");
Console.WriteLine("My first windows servce");
return true;
}
Basic settings can be changed on Program.cs
.
You should start by setting the service Name, Display Name and Description.
const string serviceName = "YourService";
const string displayName = "Your Service Name";
const string description = "A .NET Windows Service.";
Comment or uncomment other options to change defaults used during the service installation such as service identity and more.
//=> Service Identity
x.RunAsLocalSystem();
//x.RunAs("username", "password");
//x.RunAsNetworkService();
//=> Service Settings
//x.StartAutomatically();
The created project will have a dependency on Nuget packages for the the following tools, framework and/or libraries:
- TopShelf: used to create the Windows Service.
- Common.Logging: used to abstract the log implementation.
- log4net: used as the concrete log framework.
- Ninject: used for dependency injection.
These are the project defaults:
- Daily log to a text file under the 'Logs' directory.
- Maximum 5MB per log file.
- Maximum 30 days of log files.
- Log level set to INFO.
- Log to console when service is running as a command line application.
- Windows Event Log appender fully configured but disabled.
To enable Windows Event log open file NLog.config
and uncomment the line referring to EventLogAppender
To change default log level to INFO, just change the level on the same block from TRACE
to INFO
.
<root>
<level value="TRACE"/>
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileAppender"/>
<!--<appender-ref ref="EventLogAppender"/>-->
</root>