[Quartz.NET]: Configure NLog for logging

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

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="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <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 appearUnable 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 Reply