quartz.net的真实使用(待续)

本文介绍了一个使用Quartz.NET实现的每分钟写入一次日志到文件的功能。该功能通过定义一个任务并设置触发器来定期执行日志记录操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能:每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

(未完,待续)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值