简单实现Windows服务 TopShelf
Posted zslm___
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单实现Windows服务 TopShelf相关的知识,希望对你有一定的参考价值。
Nugut安装 log4net 和 topShelf
1)ServiceRunner类
using log4net;
using Topshelf;
class ServiceRunner : ServiceControl//, ServiceSuspend
{
private Timer _timer = null;
readonly ILog _log = LogManager.GetLogger(typeof(ServiceRunner));
public ServiceRunner()
{
double interval = 5000;
_timer = new Timer(interval);
_timer.Elapsed += new ElapsedEventHandler(OnTick);
}
protected virtual void OnTick(object sender, ElapsedEventArgs e)
{
_log.Debug("Tick:" + DateTime.Now.ToLongTimeString());
}
public bool Start(HostControl hostControl)
{
this._timer = new Timer(1000);
this._timer.Elapsed += this.OnTick;
return true;
}
public bool Stop(HostControl hostControl)
{
this._timer.Enabled = false;
this._timer.AutoReset = false;
return true;
}
public void Start()
{
_log.Info("SampleService is Started");
_timer.AutoReset = true;
_timer.Enabled = true;
_timer.Start();
}
public void Stop()
{
_log.Info("SampleService is Stopped");
_timer.AutoReset = false;
_timer.Enabled = false;
}
}
然后在Main函数中调用
static void Main(string[] args)
{
InitLog4Net();
var logger = LogManager.GetLogger(typeof(Program));
logger.Info("消息");
HostFactory.Run(x =>
{
x.UseLog4Net("~/log4net.config");
x.Service<ServiceRunner>();
x.SetDescription("Sample Topshelf Host");
x.SetDisplayName("Stuff");
x.SetServiceName("Stuff");
x.RunAsLocalSystem();
x.EnablePauseAndContinue();
});
}
private static void InitLog4Net()
{
var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
XmlConfigurator.ConfigureAndWatch(logCfg);
}
log4net.config
<?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:\Log\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".read.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>
编译成功后
在Cmd.exe里执行安装 ServiceRunner.exe install
卸载 ServiceRunner.exe uninstall
运行就用 ServiceRunner.exe start
以上是关于简单实现Windows服务 TopShelf的主要内容,如果未能解决你的问题,请参考以下文章
Windows 上静态编译 Libevent 2.0.10 并实现一个简单 HTTP 服务器(无数截图)
Windows 上静态编译 Libevent 2.0.10 并实现一个简单 HTTP 服务器(图文并茂,还有实例下载)
ubuntu 12.04 简单配置samba服务,实现主机与虚拟机互通(设置Windows虚拟盘)
#yyds干货盘点# linux iscsi 简单实现windows文件互通和实现多路径访问,并实现负载均衡高可用超详细配置方法和原理的个人理解