功能:每1分钟去向一个文件里写日志
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();
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);
IJobDetail job = JobBuilder.Create<WriteLogJob>()
.WithIdentity("job1", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(runTime)
.Build();
_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:
管理界面:
https:
(未完,待续)