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 2.0.0.2
- Common.Logging.NLog10 is linked against NLog 1.0.0.505
So you need to install correct version of Common.Logging.NLog and NLog.
- Create Console Application
-
Install Quartz.NET from NutGet. By default at this time NuGet will install following packages. By default Quartz use Common.Logging v2.1.2
<packages> <package id="Common.Logging" version="2.1.2" targetFramework="net45" /> <package id="Quartz" version="2.2.4" targetFramework="net45" /> </packages>
-
Add the configuration code below to configure NLog in the App.config file
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" /> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20"> <arg key="configType" value="INLINE" /> </factoryAdapter> </logging> </common> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target name="file" xsi:type="File" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" fileName="${basedir}/logs/logfile.txt" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="file" /> </rules> </nlog> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
-
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"); } }
-
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(); ////////////////// try { var startTime = DateTimeOffset.Now.AddSeconds(5); var job = JobBuilder.Create<SimpleJob>() .WithIdentity("job1", "group1") .Build(); var trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(startTime) .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(4)) .Build(); sched.ScheduleJob(job, trigger); sched.Start(); Thread.Sleep(TimeSpan.FromSeconds(30)); } finally { sched.Shutdown(true); }
- When press F5 to run the application following error will appearUnable to create type ‘Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20’
-
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
<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="2.0.0.2000" targetFramework="net45" /> <package id="Quartz" version="2.2.4" targetFramework="net45" /> </packages>
- 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!