Console program into a windows service

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

 

 

  1. StartType property value is set to Automatic, so when the computer is restarted, the service will start automatically.
  2. Description property is a description of the service.
  3. 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:

 

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

 

Guess you like

Origin www.cnblogs.com/djd66/p/11408381.html