版权声明:本文为马立弘原创文章,欢迎引用,谢绝转载。 https://blog.csdn.net/manimanihome/article/details/53471807
功能:每1分钟去向一个文件里写日志
/// <summary>
/// 要调度的功能模块
/// </summary>
public class WriteLogJob : IJob
{
public void Execute(IJobExecutionContext context)
{
string fileLogPath = AppDomain.CurrentDomain.BaseDirectory;
string fileLogName = "TestQuartz_" + DateTime.Now.ToLongDateString() + "_log.txt";
FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter strwriter = new StreamWriter(fs);
//设置写数据流的起始位置为文件流的末尾
strwriter.BaseStream.Seek(0, SeekOrigin.End);
//写入相关记录信息
strwriter.WriteLine("发生时间: " + DateTime.Now.ToString());
strwriter.WriteLine("---------------------------------------------");
strwriter.WriteLine();
//清空缓冲区内容,并把缓冲区内容写入基础流
strwriter.Flush();
strwriter.Close();
fs.Close();
}
}
}
public class WriteLogScheduler
{
static ISchedulerFactory _sf = new StdSchedulerFactory();
static IScheduler _sched = _sf.GetScheduler();
static WriteLogScheduler _instance = null;
static object lockObj = new object();
/// <summary>
/// 线程安全的单例对象
/// </summary>
public static WriteLogScheduler Instance
{
get
{
if (_instance == null)
{
lock (lockObj)
{
if (_instance == null)
{
_instance = new WriteLogScheduler();
}
}
}
return _instance;
}
}
public void Start()
{
ILog log = LogManager.GetLogger(typeof(WriteLogScheduler));
DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<WriteLogJob>()
.WithIdentity("job1", "group1")
.Build();
// Trigger the job to run on the next round minute
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(runTime)
.Build();
// Tell quartz to schedule the job using our trigger
_sched.ScheduleJob(job, trigger);
_sched.Start();
}
public void Stop()
{
_sched.Shutdown(true);
}
}
protected void Application_Start()
{
WriteLogScheduler.Instance.Start();
}
protected void Application_End(object sender, EventArgs e)
{
WriteLogScheduler.Instance.Stop();
}
WEB.Config的configuration节点里做一些必要的配置
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="showLogName" value="true"/>
<arg key="showDataTime" value="true"/>
<arg key="level" value="INFO"/>
<arg key="dateTimeFormat" value="HH:mm:ss:fff"/>
</factoryAdapter>
</logging>
</common>
<quartz>
<add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="10"/>
<add key="quartz.threadPool.threadPriority" value="2"/>
<add key="quartz.jobStore.misfireThreshold" value="60000"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
参考:
http://www.cnblogs.com/mushroom/p/4231642.html
管理界面:
https://github.com/guryanovev/CrystalQuartz
(未完,待续)