zoukankan      html  css  js  c++  java
  • Quartz.Net 学习随手记之01 初步介绍

    一、Quartz.net

    1. 简单说明:Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems,更多内容请移步 http://quartznet.sourceforge.net/
    2. 下载地址:http://quartznet.sourceforge.net/download.html (根据需要请自行下载)
    3. 一个示例:本示例仅仅是为了说明Quartz.net作为一个作业调度框架,其使用是多么简单。
    class Program
    {
        static void Main(string[] args)
        {
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            IScheduler scheduler = schedulerFactory.GetScheduler();
            scheduler.Start();
    
            JobDetail job = new JobDetail("MyFirstDailyJob", typeof(DailyJob));
    
            Trigger trigger = TriggerUtils.MakeSecondlyTrigger(1);
            trigger.StartTimeUtc = TriggerUtils.GetEvenSecondDate(DateTime.UtcNow);
            trigger.Name = "DailyJob";
    
            scheduler.ScheduleJob(job, trigger);
        }
    }

    具体的Job内容

    public class DailyJob : IJob
        {
            public void Execute(JobExecutionContext context)
            {
                //ILog log4net = LogManager.GetLogger("LogFileAppender");
                //log4net.Info(DateTime.Now);
                Console.WriteLine(DateTime.Now);
            }
        }

    二、log4net

    1. 简单说明:The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .NET runtime,更多内容请自行移步http://logging.apache.org/log4net/
    2. 下载网站:http://logging.apache.org/log4net/download.html
    3. 一个示例:本示例基于配置文件
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <configSections>
        <sectionGroup name="common">
          <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </configSections>
    
      <common>
        <logging>
          <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
            <arg key="configType" value="INLINE"/>
          </factoryAdapter>
        </logging>
      </common>
    
      <log4net>
        <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
          <param name="File" value="MyQuartzLog.txt" />
          <param name="AppendToFile" value="true" />
          <rollingStyle value="Size" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="10MB" />
          <staticLogFileName value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
          </layout>
        </appender>
        <root>
          <level value="INFO" />
          <appender-ref ref="LogFileAppender" />
        </root>
      </log4net>
    
    </configuration>

    自定义调用

    ILog log4net = LogManager.GetLogger("LogFileAppender");
    log4net.Info(DateTime.Now);

    输出结果

    INFO 2012-04-10 04:50:27 – Default Quartz.NET properties loaded from embedded resource file
    INFO 2012-04-10 04:50:27 – Initialized Scheduler Signaller of type: Quartz.Core.SchedulerSignalerImpl
    INFO 2012-04-10 04:50:27 – Quartz Scheduler v.1.0.3.2 created.
    INFO 2012-04-10 04:50:27 – RAMJobStore initialized.
    INFO 2012-04-10 04:50:27 – Quartz scheduler 'DefaultQuartzScheduler' initialized
    INFO 2012-04-10 04:50:27 – Quartz scheduler version: 1.0.3.2
    INFO 2012-04-10 04:50:27 – Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
    DEBUG2012-04-10 04:50:28 – Producing instance of Job 'DEFAULT.MyFirstDailyJob', class=QuartzNETConsoleApp.DailyJob
    DEBUG2012-04-10 04:50:28 – Calling Execute on job DEFAULT.MyFirstDailyJob
    INFO 2012-04-10 04:50:28 – 2012-4-10 16:50:28
    DEBUG2012-04-10 04:50:28 – Trigger instruction : NoInstruction
    DEBUG2012-04-10 04:50:29 – Producing instance of Job 'DEFAULT.MyFirstDailyJob', class=QuartzNETConsoleApp.DailyJob
    DEBUG2012-04-10 04:50:29 – Calling Execute on job DEFAULT.MyFirstDailyJob
    INFO 2012-04-10 04:50:29 – 2012-4-10 16:50:29
    DEBUG2012-04-10 04:50:29 – Trigger instruction : NoInstruction
    DEBUG2012-04-10 04:50:30 – Producing instance of Job 'DEFAULT.MyFirstDailyJob', class=QuartzNETConsoleApp.DailyJob
    DEBUG2012-04-10 04:50:30 – Calling Execute on job DEFAULT.MyFirstDailyJob
  • 相关阅读:
    [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
    [LeetCode] Number of Segments in a String 字符串中的分段数量
    [LintCode] Longest Common Prefix 最长共同前缀
    [LintCode] Product of Array Except Self 除本身之外的数组之积
    [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
    [LintCode] Sort List 链表排序
    [LintCode] Find Peak Element 求数组的峰值
    [LintCode] Container With Most Water 装最多水的容器
    [LintCode] Linked List Cycle 单链表中的环
    [LeetCode] 465. Optimal Account Balancing 最优账户平衡
  • 原文地址:https://www.cnblogs.com/panchunting/p/Quartznet_log4net_demo.html
Copyright © 2011-2022 走看看