入门例子
using log4net; using System; using System.Timers; using Topshelf;namespace ConsoleAppTest {class Program{static void Main(string[] args){log4net.Config.XmlConfigurator.Configure();HostFactory.Run(x =>{x.Service<TownCrier>();x.RunAsLocalSystem(); // 服务使用NETWORK_SERVICE内置帐户运行。身份标识,有好几种方式,如://x.RunAs("username", "password"); x.RunAsPrompt(); x.RunAsNetworkService(); x.SetServiceName("ConsoleAppTest服务名称");x.SetDisplayName("ConsoleAppTest显示名称");x.SetDescription("ConsoleAppTest服务描述");});}}public class TownCrier : ServiceControl{readonly Timer _timer;readonly ILog _log = log4net.LogManager.GetLogger("wolflogInfo");public TownCrier(){//10秒运行一次_timer = new Timer(10000) { AutoReset = true };_timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);}bool ServiceControl.Start(HostControl hostControl){_log.Info("ConsoleAppTest is Started");_timer.Start();return true;}bool ServiceControl.Stop(HostControl hostControl){_log.Info("ConsoleAppTest is Stop");_timer.Start();return true;}}}
Log4的config配置
<?xml version="1.0" encoding="utf-8" ?> <configuration><configSections><section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></configSections><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup><log4net><logger name="wolflogInfo"><level value="Info" /><appender-ref ref="wolflogInfoFileAppender" /></logger><logger name="wolflogError"><level value="Error" /><appender-ref ref="wolflogErrorFileAppender" /></logger><logger name="wolflogALL"><level value="ALL" /><appender-ref ref="wolflogALLFileAppender" /></logger><!-- 绑定使用的名称 --> <appender name="wolflogInfoFileAppender" type="log4net.Appender.RollingFileAppender,log4net"><lockingModel type="log4net.Appender.FileAppender+MinimalLock" /><!--输出文件夹路径--><param name="File" type="" value="App_Data/Log/Info/" /><param name="AppendToFile" value="true" /><param name="RollingStyle" value="Date" /><!--文件名称命名规则--><param name="DatePattern" value="yyyyMMdd".txt"" /><param name="StaticLogFileName" value="false" /><param name="MaxSizeRollBackups" value="62" /><layout type="log4net.Layout.PatternLayout,log4net"><!--输出头--><param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /></layout></appender><appender name="wolflogErrorFileAppender" type="log4net.Appender.RollingFileAppender,log4net"><lockingModel type="log4net.Appender.FileAppender+MinimalLock" /><param name="File" type="" value="App_Data/Log/Error/" /><param name="AppendToFile" value="true" /><param name="RollingStyle" value="Date" /><param name="DatePattern" value="yyyyMMdd".txt"" /><param name="StaticLogFileName" value="false" /><param name="MaxSizeRollBackups" value="62" /><layout type="log4net.Layout.PatternLayout,log4net"><param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /></layout></appender><appender name="wolflogALLFileAppender" type="log4net.Appender.RollingFileAppender,log4net"><lockingModel type="log4net.Appender.FileAppender+MinimalLock" /><param name="File" type="" value="App_Data/Log/ALL/" /><param name="AppendToFile" value="true" /><param name="RollingStyle" value="Date" /><param name="DatePattern" value="yyyyMMdd".txt"" /><param name="StaticLogFileName" value="false" /><param name="MaxSizeRollBackups" value="62" /><layout type="log4net.Layout.PatternLayout,log4net"><param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /></layout></appender></log4net> </configuration>
安装运行命令
安装:ConsoleAppTest install
卸载:ConsoleAppTest uninstall
启动:ConsoleAppTest start
停止:ConsoleAppTest stop
需要用管理员运行程序
多实例安装
ConsoleAppTest –instance "ConsoleAppTest2" install
ConsoleAppTest –instance "ConsoleAppTest2" uninstall
也可以 sc delete 删除