basquang™ on clouds

August 4, 2014

[Quartz.NET]: Configure Logging

Filed under: Logging,Quartz,Quartz.NET — basquang @ 5:02 PM
Tags: ,

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
  3. //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);
    

  4. 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

Advertisements

2 Comments »

  1. This text is worth everyone’s attention. When can I find out more?

    Comment by tips for families home — August 26, 2014 @ 5:34 PM | Reply

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

    Comment by földmunkagép — October 18, 2014 @ 11:22 PM | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: