basquang™ on clouds

August 4, 2014

[Quartz.NET]: Configure NLog for logging

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

In previous article Configure Logging I described how to configure Logging in configuration file using Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter.

In this article, I will describe how to configure NLog in Quartz application.

Because Quartz.NET using Common.Logging frameworks, so there are two implementations dependency when you want to use NLogLoggerFactoryAdapter:

  • Common.Logging.NLog20 is linked against NLog
  • Common.Logging.NLog10 is linked against NLog

So you need to install correct version of Common.Logging.NLog and NLog.

  1. Create Console Application
  2. Install Quartz.NET from NutGet. By default at this time NuGet will install following packages. By default Quartz use Common.Logging v2.1.2

      <package id="Common.Logging" version="2.1.2" targetFramework="net45" />
      <package id="Quartz" version="2.2.4" targetFramework="net45" />

  3. Add the configuration code below to configure NLog in the App.config file

    <?xml version="1.0" encoding="utf-8"?>
        <sectionGroup name="common">
          <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
          <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
            <arg key="configType" value="INLINE" />
      <nlog xmlns="" xmlns:xsi="">
          <target name="file" xsi:type="File" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" fileName="${basedir}/logs/logfile.txt" />
          <logger name="*" minlevel="Debug" writeTo="file" />
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

  4. Create simple Job like this

    public class SimpleJob : IJob
            private static ILog logging = LogManager.GetLogger(typeof(SimpleJob));
            public void Execute(IJobExecutionContext context)
                logging.InfoFormat("Hello from job");

  5. Using the job in Program

    ILog log = LogManager.GetLogger(typeof(Program));
    // First we must get a reference to a scheduler
    ISchedulerFactory sf = new StdSchedulerFactory();
    IScheduler sched = sf.GetScheduler();
    	var startTime = DateTimeOffset.Now.AddSeconds(5);
    	var job = JobBuilder.Create<SimpleJob>()
    						.WithIdentity("job1", "group1")
    	var trigger = TriggerBuilder.Create()
    		.WithIdentity("trigger1", "group1")
    		.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(4))
    	sched.ScheduleJob(job, trigger);       

  6. When press F5 to run the application following error will appear

    Unable to create type ‘Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20’

  7. To resolve this issue. Install Common.Logging.NLog20 from NuGet by running following command in order.NOTE: To use NLong we need to update Common.Logging to version 2.2.0

    Install-Package Common.Logging.NLog20

    Install-Package Common.Logging.Core

    Install-Package Common.Logging -Version 2.2.0

    This command will install following packages

      <package id="Common.Logging" version="2.2.0" targetFramework="net45" />
      <package id="Common.Logging.Core" version="2.2.0" targetFramework="net45" />
      <package id="Common.Logging.NLog20" version="2.2.0" targetFramework="net45" />
      <package id="NLog" version="" targetFramework="net45" />
      <package id="Quartz" version="2.2.4" targetFramework="net45" />

  8. Now run application again and see the output of logged file in the path bin\debug\logs\logfile.txt

The logged file created by NLog in the configuration you have defined above.

Hope this help!



Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Blog at

%d bloggers like this: