前面講了如何使用Topshelf 快速開發windows服務, 不清楚的能夠看以前的這篇文章:https://www.cnblogs.com/zhangweizhong/category/771057.html,html
今天說一說Topshelf 的相關配置。windows
簡單配置
官方文檔,對HostFactory 裏面的參數作了詳細的說明:http://docs.topshelf-project.com/en/latest/configuration/config_api.html ,下面只對一些經常使用的方法進行簡單的解釋:api
咱們將上面的程序代碼改一下:app
HostFactory.Run(x => //1 { x.Service<TownCrier>(s => //2 { s.ConstructUsing(name => new TownCrier()); //配置一個徹底定製的服務,對Topshelf沒有依賴關係。經常使用的方式。 //the start and stop methods for the service s.WhenStarted(tc => tc.Start()); //4 s.WhenStopped(tc => tc.Stop()); //5 }); x.RunAsLocalSystem(); // 服務使用NETWORK_SERVICE內置賬戶運行。身份標識,有好幾種方式,如:x.RunAs("username", "password"); x.RunAsPrompt(); x.RunAsNetworkService(); 等 x.SetDescription("Sample Topshelf Host服務的描述"); //安裝服務後,服務的描述 x.SetDisplayName("Stuff顯示名稱"); //顯示名稱 x.SetServiceName("Stuff服務名稱"); //服務名稱 });
重裝安裝運行後:spa
經過上面,相信你們都很清楚 SetDescription、SetDisplayName、SetServiceName區別。再也不細說。日誌
服務配置
Topself的服務通常有主要有兩種使用模式。code
1、簡單模式。繼承ServiceControl接口,實現該接口便可。
實例:xml
namespace TopshelfDemo { public class TownCrier : ServiceControl { private Timer _timer = null; readonly ILog _log = LogManager.GetLogger(typeof(TownCrier)); public TownCrier() { _timer = new Timer(1000) { AutoReset = true }; _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now); } public bool Start(HostControl hostControl) { _log.Info("TopshelfDemo is Started"); _timer.Start(); return true; } public bool Stop(HostControl hostControl) { throw new NotImplementedException(); } } class Program { public static void Main(string[] args) { var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); XmlConfigurator.ConfigureAndWatch(logCfg); HostFactory.Run(x => { x.Service<TownCrier>(); x.RunAsLocalSystem(); x.SetDescription("Sample Topshelf Host服務的描述"); x.SetDisplayName("Stuff顯示名稱"); x.SetServiceName("Stuff服務名稱"); }); } } }
2、經常使用模式。
實例:htm
namespace TopshelfDemo { public class TownCrier { private Timer _timer = null; readonly ILog _log = LogManager.GetLogger(typeof(TownCrier)); public TownCrier() { _timer = new Timer(1000) { AutoReset = true }; _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now); } public void Start() { _timer.Start(); } public void Stop() { _timer.Stop(); } } class Program { public static void Main(string[] args) { var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); XmlConfigurator.ConfigureAndWatch(logCfg); HostFactory.Run(x => { x.Service<TownCrier>(s => { s.ConstructUsing(name => new TownCrier()); s.WhenStarted(tc => tc.Start()); s.WhenStopped(tc => tc.Stop()); }); x.RunAsLocalSystem(); x.SetDescription("Sample Topshelf Host服務的描述"); x.SetDisplayName("Stuff顯示名稱"); x.SetServiceName("Stuff服務名稱"); }); } } }
兩種方式,都使用了Log4Net,相關配置:blog
<? xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name = "log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name = "RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日誌路徑--> <param name = "File" value= "D:\App_Data\servicelog\"/> <!--是不是向文件中追加日誌--> <param name= "AppendToFile" value= "true"/> <!--log保留天數--> <param name= "MaxSizeRollBackups" value= "10"/> <!--日誌文件名是不是固定不變的--> <param name= "StaticLogFileName" value= "false"/> <!--日誌文件名格式爲:2008-08-31.log--> <param name= "DatePattern" value= "yyyy-MM-dd".log=""""/> <!--日誌根據日期滾動--> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d[% t] %-5p %c - %m%n %loggername" /> </layout> </appender> <!-- 控制檯前臺顯示日誌 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="Info" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender> <root> <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <level value="all" /> <appender-ref ref="ColoredConsoleAppender"/> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net> </configuration>
推薦使用第二種經常使用模式。