开发windows服务,除了在vs里新建服务项目外(之前有写过具体开发方法,可点击查看),还可以使用Topshelf。

不过使用topshelf需要.netframework 4.5.2版本,在vs2013上引用不成功,我这里使用的是vs2017。

以下为具体步骤:

一、引用topshelf 并使用

1、在vs里新建控制台程序

2、在引用里使用NuGet搜索topshelf并安装

3、程序代码

using log4net;
using System;
using System.IO;
using System.Reflection;
using System.Timers;
using Topshelf; namespace TopshelfTest
{
public class TestWriteLog
{
readonly Timer _timer;
ILog _log = LogManager.GetLogger(typeof(TestWriteLog));
public TestWriteLog()
{
_timer = new Timer()
{
AutoReset = true,
Enabled = true
};
int i = ;
_timer.Elapsed += delegate (object sender, System.Timers.ElapsedEventArgs e)
{
i++;
_log.Info("测试" + i.ToString()); };
} public void Start()
{
_log.Info("Service is Started");
_timer.Start();
}
public void Stop()
{
_log.Info("Service is Stopped");
_timer.Stop();
} /// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{ HostFactory.Run(c =>
{
c.Service<TestWriteLog>((x) =>
{
x.ConstructUsing(name => new TestWriteLog());
x.WhenStarted((t) => t.Start());
x.WhenStopped((t) => t.Stop());
});
c.RunAsLocalSystem();
//服务描述
c.SetDescription("TestServices测试服务描述");
//服务显示名称
c.SetDisplayName("TestServices测试服务显示名称");
//服务的真实名称
c.SetServiceName("TestServices");
});
}
}
}

4、log4net引用和配置方法

  4.1 使用以上方法引用log4net.dll

  4.2 在app.config 里作如下配置

 <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections> <log4net>
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<!-- Set root logger level to ERROR and its appenders -->
<root>
<level value="ALL" />
<appender-ref ref="SysAppender" />
</root>
<!-- Print only messages of level DEBUG or above in the packages -->
<logger name="WebLogger">
<level value="log" />
</logger>
<appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
<!--<param name="File" value="App_Data/" />-->
<File value="Logs\log" />
<!--日志文件位置和文件名-->
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<!--<param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />-->
<param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
<!--在文件名后面加内容 比如 log名变为log20180808-->
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>

  4.3 在properties的AssemblyInfo.cs里 加如下配置

[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

5、生成项目

生成项目后可以将bin\debug里的内容拷出来单独放服务的位置,

6、安装、卸载服务

使用管理员运行cmd,cd到你的服务文件位置,我这里直接在debug里安装服务的。

 服务.exe install     -----安装服务

在服务里就可以看到安装的服务了

 服务.exe uninstall     -----卸载服务  

 7、运行结果 

在安装服务后开启服务 

最新文章

  1. FreeMaker实现变量求和
  2. Android 下进行单元测试 Test run failed:Instrumentation run failed due to &#39;java.lang.ClassNotFoundException&#39;
  3. git --如何撤销已放入缓存区(Index区)的修改
  4. angularjs + seajs构建Web Form前端(三) -- 兼容easyui
  5. Jenkins进阶系列之——13修改Jenkins权限控制
  6. Ubuntu 14.04搭建简单git服务器
  7. python2.7系列安装失败的办法
  8. JavaScript 链式结构序列化详解
  9. Centos6.5安装
  10. ASP.net:截取固定长度字符串显示在页面,多余部分显示为省略号
  11. android 控件的移动
  12. c 输出9x9乘法口诀表 这个学for循环绕不开的一题
  13. 【原创】构建高性能ASP.NET站点之三 细节决定成败
  14. 73、django之setting配置汇总
  15. 2019-2-13TextBox技巧
  16. Redis安全以及备份还原
  17. [20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED2.txt
  18. [UI] 04 - Bootstrap: layout &amp; navigation
  19. 梦殇 chapter one
  20. Construct Binary Tree from Preorder and Inorder Traversal leetcode java

热门文章

  1. C# 自动程序 windows 无法启动 XXXX 服务 错误5 拒绝访问
  2. ubuntu16.04下安装g2o
  3. Reader和Writer
  4. Django 搭建博客记(二)
  5. 深入理解JVM(二)——内存模型、可见性、指令重排序
  6. ElasticSearch是如何实现分布式的?
  7. 从零开始单排学设计模式「简单工厂设计模式」黑铁 III
  8. node.js服务器搭建
  9. 吴恩达机器学习笔记23-神经网络:表述--非线性假设(Non-linear Hypotheses)
  10. 吴恩达机器学习笔记22-正则化逻辑回归模型(Regularized Logistic Regression)