Create a Windows service using Topshelf

Source: Internet
Author: User
Tags log4net

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&quot;". log&quot; "/>    <!--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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.