一、项目开发
1.新建控制台应用(.NET Framework)

2.配置新项目,自行修改项目名称、位置和框架(建议使用.NET Framework4.5以上版本)

创建好的项目目录如下:

3.右键引用,点击添加引用(R)...

4.引用管理器->程序集->框架,添加System.Configuration引用

点击确定,即添加引用到项目中。

5.右键引用,点击管理NuGet程序包(N)...

按照以下顺序添加包:
|
包名 |
版本 |
依赖项 |
|
Common.Logging.Core |
3.3.1 |
|
|
Common.Logging |
3.3.1 |
Common.Logging.Core (>= 3.3.1) |
|
Quartz |
2.6.2 |
Common.Logging (>= 3.3.1) |
|
log4net |
2.0.5 |
|
|
Topshelf |
3.3.1 |
|
|
Topshelf.Log4Net |
3.3.1 |
Topshelf (>= 3.3.1) log4net (>= 2.0.5) |
添加完毕后如图:

6.添加配置文件
添加配置文件步骤如下:


(1)log4net.config
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
(2)quartz.config
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # You can configure your scheduler in either # or in quartz properties file# Configuration section has precedencequartz.scheduler.instanceName = QuartzTest# configure thread pool infoquartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount = 10quartz.threadPool.threadPriority = Normal# job initialization plugin handles our xml reading, without it defaults are usedquartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartzquartz.plugin.xml.fileNames = ~/quartz_jobs.xml# export this server to remoting context#quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz#quartz.scheduler.exporter.port = 555#quartz.scheduler.exporter.bindName = QuartzScheduler#quartz.scheduler.exporter.channelType = tcp#quartz.scheduler.exporter.channelName = httpQuartz |
(3)quartz_jobs.xml
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | true <group>Alarmgroup> true false <group>Alarmgroup> |
选中三个文件->右键->属性,复制到输出目录:如果较新则复制

7.添加服务运行类:ServiceRunner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | namespace AlarmService{ public class ServiceRunner : Topshelf.ServiceControl, Topshelf.ServiceSuspend { private readonly Quartz.IScheduler scheduler; public ServiceRunner() { scheduler = Quartz.Impl.StdSchedulerFactory.GetDefaultScheduler(); } public bool Start(Topshelf.HostControl hostControl) { scheduler.Start(); return true; } public bool Stop(Topshelf.HostControl hostControl) { scheduler.Shutdown(false); return true; } public bool Continue(Topshelf.HostControl hostControl) { scheduler.ResumeAll(); return true; } public bool Pause(Topshelf.HostControl hostControl) { scheduler.PauseAll(); return true; } }} |
8.Program.cs中增加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System;using Topshelf;namespace AlarmService{ class Program { static void Main(string[] args) { log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config")); Topshelf.HostFactory.Run(x => { x.UseLog4Net(); x.Service x.RunAsLocalSystem(); x.SetDescription("Quartz+TopShelf implements a Windows service that to achieve timing task scheduling."); x.SetDisplayName("Alarm"); x.SetServiceName("AlarmService"); x.EnablePauseAndContinue(); }); } }} |
9.增加帮助类:CommonHelper
1 2 3 4 5 6 7 | namespace AlarmService{ public class CommonHelper { public static readonly log4net.ILog AppLogger = log4net.LogManager.GetLogger("AppLogger"); }} |
10.添加文件夹:QuartzJobs

在QuartzJobs文件夹下新建类:AlarmJob

该类继承Quartz.IJob,实现接口,如下图:

AlarmJob类完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 | namespace AlarmService.QuartzJobs{ public sealed class AlarmJob : Quartz.IJob { public void Execute(Quartz.IJobExecutionContext context) { CommonHelper.AppLogger.InfoFormat("AlarmJob开始..."); //TODO 业务逻辑 CommonHelper.AppLogger.InfoFormat("AlarmJob结束"); } }} |
二、测试
在项目的bin/Debug下新建Log文件夹。F5启动项目

进入断点

控制台显示日志:

Log文件夹下的日志:

三、安装服务
1.找一台服务器,把项目目录下bin\Debug的文件夹复制到服务器某个磁盘下,windows键输入cmd,显示命令提示符,右键命令提示符,以管理员身份运行,在窗体中输入“cd C:\xxx”,(xxx为AlarmService.exe所在的文件夹),回车后继续输入“AlarmService.exe install”,显示已完成事务处理安装。
2.win+R键打开运行,输入“services.msc”,点击确定,打开服务,找到AlarmService,右键“启动(S)”即可。
3.在xxx\Log下即可看到相应的日志。
源码网盘地址:
链接:https://pan.baidu.com/s/1m62Pu44KypipGVBukDhNrA
提取码:08g2