Topshelf is an open-source, cross-platform hosting service framework that supports Windows and mono and can build a convenient service host with just a few lines of code.
Official website: http://topshelf-project.com/
Document: https://topshelf.readthedocs.org/en/latest/
Code: Https://github.com/topshelf/topshelf
This article references: http://www.cnblogs.com/jys509/p/4614975.html
1. Create a new console project that references Topshelf and log4net:
install-Packagetopshelfinstall-package topshelf.log4net
2. Modify the relevant documents.
Program.cs:
usingSystem;usingSystem.IO;usingsystem.timers;usinglog4net;usinglog4net. Config;usingtopshelf;namespacetopshelfdemo{ Public classTowncrier {ReadOnlyTimer _timer; ReadOnlyILog _log = Logmanager.getlogger (typeof(Towncrier)); PublicTowncrier () {_timer=NewTimer ( the) {AutoReset =true }; _timer. Elapsed+ = (sender, EventArgs) ={_log. Info (DateTime.Now); }; } Public voidStart () {_timer. Start (); } Public voidStop () {_timer. Stop (); } } classProgram { Public Static voidMain () {varLogcfg =NewFileInfo (AppDomain.CurrentDomain.BaseDirectory +"Log4net.xml"); Xmlconfigurator.configureandwatch (LOGCFG); Hostfactory.run (x={X.service<TownCrier> (s ={s.constructusing (name=Newtowncrier ()); s.whenstarted (TC=TC. Start ()); s.whenstopped (TC=TC. Stop ()); }); X.runaslocalsystem (); X.setdescription ("Sample topshelf Host"); X.setdisplayname ("Stuff"); X.setservicename ("Stuff"); }); } }}
Log4net.xml:
<?XML version= "1.0" encoding= "Utf-8"?><log4net> <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender "> <!--Log Path - <paramname= "File"value= "Logs\"/> <!--whether to append the log to the file - <paramname= "Appendtofile"value= "true"/> <!--Log retention days - <paramname= "Maxsizerollbackups"value= "Ten"/> <!--whether the log file name is fixed - <paramname= "Staticlogfilename"value= "false"/> <!--the log file name format is: 2008-08-31.log - <paramname= "Datepattern"value= "Yyyy-mm-dd"". log" "/> <!--log scrolling by date - <paramname= "Rollingstyle"value= "Date"/> <Layouttype= "log4net." Layout.patternlayout "> <paramname= "Conversionpattern"value= "[%d] [%p] [%c]-%m%n" /> </Layout> </Appender> <!--Console Display Log - <Appendername= "Coloredconsoleappender"type= "log4net." Appender.coloredconsoleappender "> <Mapping> < Levelvalue= "ERROR" /> <ForeColorvalue= "Red, highintensity" /> </Mapping> <Mapping> < Levelvalue= "Info" /> <ForeColorvalue= "Green" /> </Mapping> <Layouttype= "log4net." Layout.patternlayout "> <Conversionpatternvalue= "%n%date{hh:mm:ss,fff} [%-5level]%m" /> </Layout> <Filtertype= "log4net." Filter.levelrangefilter "> <paramname= "Levelmin"value= "Info" /> <paramname= "Levelmax"value= "Fatal" /> </Filter> </Appender> <Root> <!--(High) OFF > FATAL > ERROR > WARN > INFO > DEBUG > All (Low) - < Levelvalue= "All" /> <Appender-refref= "Coloredconsoleappender"/> <Appender-refref= "Rollinglogfileappender"/> </Root></log4net>
3. Execute, display a time message every 5 seconds in the console and log files:
4. Service-related operations:
Installation: TopshelfDemo.exe Install
Start: TopshelfDemo.exe start
Uninstall: TopshelfDemo.exe Uninstall
Create a Windows service using Topshelf