[Quartz.NET]: Configure Logging

  • Post author:
  • Post category:Quartz
  • Post comments:3 Comments
  • Reading time:3 mins read

Common.Logging can be configured to use different logging frameworks under the hood; namely Enterprise Library, Log4Net and NLog.

However, to keep things simple in last post Get Started we take the simple route and configure logging using code to just log to the console using Common.Logging basic logging mechanism.

Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info};

This blog post describe how to configure Logging in application configuration file.

  1. Create Console Application
  2. Install Quartz.NET from NuGet
  3. Add following code in the App.config to configure Logging for Quart. In this logging configuration we use ConsoleOutLoggerFactoryAdapter to display log to the Console.Out screen.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <configSections>   
    <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>
  
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
    1. Add a simple Job like this. In this Job we use log the execution of the Job by writing the message “Hello World…”
      public class HelloJob : IJob
          {
              //Get logging configuration
              private static ILog _log = LogManager.GetLogger(typeof(HelloJob));
      
              /// <summary> 
              /// Called by the <see cref="IScheduler" /> when a
              /// <see cref="ITrigger" /> fires that is associated with
              /// the <see cref="IJob" />.
              /// </summary>
              public virtual void Execute(IJobExecutionContext context)
              {
      
                  // Say Hello to the World and display the date/time
                  _log.Info(string.Format("Hello World! - {0}", System.DateTime.Now.ToString()));
              }
      
          }
      
    2. Using the Job in Program class as below
//Get Logging configuration
ILog log = LogManager.GetLogger(typeof(Program));

// First we must get a reference to a scheduler
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();

log.Info("------- Scheduling Job  -------------------");

// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
	.WithIdentity("job1", "group1")
	.Build();

// Trigger the job to run on the next round minute
ITrigger trigger = TriggerBuilder.Create()
	.WithIdentity("trigger1", "group1")
	.StartNow()
	.WithSimpleSchedule(x => x
	.WithIntervalInSeconds(10)
	.RepeatForever())
	.Build();

// Tell quartz to schedule the job using our trigger
sched.ScheduleJob(job, trigger);           

// Start up the scheduler (nothing can actually run until the 
// scheduler has been started)
sched.Start();

// wait long enough so that the scheduler as an opportunity to 
// run the job!

// wait 65 seconds to show jobs
Thread.Sleep(TimeSpan.FromSeconds(60));

// shut down the scheduler
sched.Shutdown(true);
  1. The Console screen output will display the message (execute the job) you have been defined each 10 seconds 6 time (within 60 seconds)

Hope this help

This Post Has 3 Comments

  1. földmunkagép

    homlokrakodó gép, munkagép, földmunkagép,időszakos
    biztonsági vizsgálat, üzembehelyezés szakértővel országosan.

Leave a Reply