First we create a console application WriteLog, then add the windows service LogService
Record new windows service switch to code view, I performed here with log4net logs
OnStart method is performed after the service starts, OnStop method is performed after the service stops.
Switch to the design page, right-click the Add Installer
Modify the properties of serviceInstaller1
- StartType property value is set to Automatic, so when the computer is restarted, the service will start automatically.
- Description property is a description of the service.
- Display Properties is the service name.
Modify serviceProcessInstaller1 property
LocalSystem indicates that the local system administrator
Console startup items Program class service running
static void the Main ( String [] args) { the ServiceBase [] ServicesToRun; ServicesToRun = new new the ServiceBase [] { new new the LogService () // the LogService here is the new Windows services }; ServiceBase.Run (ServicesToRun); }
Run the program as an administrator:
![](https://img2018.cnblogs.com/blog/913142/201908/913142-20190825163747479-1892869128.png)
Switch to the cd C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 ( or C: \ Windows \ Microsoft.NET \ Framework \ v2.0.50727)
Installation Services: installutil *** (program path)
OK, the service added successfully. cmd enter services.msc View Service
We start the service, view the log it was recorded successfully.
Logging success, add the service to complete.
Uninstall service: installutil / u *** (program path)
Author: clown high-profile
link: https: //www.jianshu.com/p/2a5410c5bbb1