Quartz.NET在ASP.NET 中使用

Quartz.NET 项目地址 http://quartznet.sourceforge.net/
common logging是一个通用日志接口,log4net是一个具体实现 ,也可以使用NLog,EntLib等(一定注意版本,我就在log4j配置上浪费了好长时间)。

private IScheduler sched;
        private readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        ///  应用程序启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Start(object sender, EventArgs e)
        {
            //初始化调度器工厂
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            //创建调度器
            sched = schedFact.GetScheduler();
            //创建任务
            IJobDetail job = JobBuilder.Create<HomeinnsUpdatePriceJob>().WithIdentity("job1", "group1").Build();
            //创建任务触发器
            ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create().WithIdentity("trigger1", "group1").WithCronSchedule(ConfigurationManager.AppSettings["cronExpr"]).Build();
            sched.ScheduleJob(job, trigger);
            sched.Start();
            logger.Info("Quartz服务成功启动");
        }
        /// <summary>
        /// 应用程序关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_End(object sender, EventArgs e)
        {
            if (sched != null)
            {
                sched.Shutdown(true);
                logger.Info("Quartz服务成功终止");
            }
        }

配置

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
    <appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log/" />
      <appendToFile value="true" />
      <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="false" />
      <Encoding value="UTF-8" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger  - %message%newline" />
      </layout>
    </appender>
    <appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log/error.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10240KB" />
      <staticLogFileName value="true" />
      <Encoding value="UTF-8" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="WARN" />
        <param name="LevelMax" value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="InfoFileAppender" />
      <appender-ref ref="ErrorFileAppender" />
    </root>
  </log4net>

  <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>
  <appSettings>
    <add key="cronExpr" value="0/10 * * * * ?" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.2.0" newVersion="2.1.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Refer:
Quartz.NET 2.0 配置文件详解
http://www.cnblogs.com/h20064528/archive/2012/07/17/2595636.html
Window服务
http://www.cnblogs.com/lzrabbit/archive/2012/04/15/2448326.html
DUDU
http://www.cnblogs.com/dudu/archive/2011/04/04/quartz_net.html
Failed obtaining configuration for Common.Logging from configuration section 'common/logging'
http://stackoverflow.com/questions/11368267/failed-obtaining-configuration-for-common-logging-from-configuration-section-co

你可能感兴趣的:(asp.net)