Wednesday, April 22, 2009

Configuring Quartz.Net to use Log4net

NOTE: I'm now blogging at http://jayvilalta.com/blog and not updating this blog anymore. For information on the latest version of Quartz.Net, visit me there.

If you’re considering using Quartz.Net, chances are you are using log4net as the logging framework for your application. We’ll assume that you already know how to configure log4net and that you just want to plug that configuration into Quartz.Net. So, how do we configure Quartz.Net to use log4net?
It’s not terribly complicated, since Quartz.Net is using Common.Logging. In this post we’ll consider 2 use cases for configuring Quartz.Net to use log4net: a standalone Quartz.Net server, and Quartz.Net embedded within your application. Fortunately for us, the process we need to follow to configure Quartz.Net is the same for both!
Configuring Quartz.Net with Log4net
To configure log4net for a standalone Quartz.Net server running as a windows service, we have to make some changes to the Quartz.Server.Service.exe.config file. This file should be in the directory where you have your Quartz.Net installation. If Quartz.Net is embedded in your application, then you will be modifying your application’s config file (web.config or yourapp.exe.config for example).
You will need to add 2 elements to the <configSections> element:
<section
name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

This bit lets you configure log4net directly from the quart configuration file. Now, add these elements:
<sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>

This lets you configure the Commons Logging from the quartz configuration file as well. All in all, your <configSections> section will look something like this:
<configSections>
  <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  <sectionGroup name="common">
    <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
  </sectionGroup>
</configSections>

So far all we have done is set up the configuration file to be able to configure both commons logging and log4net from within that one file. Now, let’s configure commons logging to use log4net. Just add this somewhere under the <configuration> element:
<common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

This tells commons logging that we are going to log to log4net. Now, all that is left to do is to configure log4net itself. Here is a sample log4net configuration:
<log4net>
   <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
     <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%d [%t] %-5p %l - %m%n" />
     </layout>
   </appender>
   <root>
     <level value="INFO" />
     <appender-ref ref="EventLogAppender" />
   </root>
</log4net>

To wrap up this post, let me point out 2 things to keep in mind when configuring log4net with Quartz.net:
  1. You don’t need to configure log4net in the main application config file. You can load an external log4net configuration file, just as you would if you weren’t using Quartz.Net.
  2. You do need to tell commons logging to use log4net as the logging framework, which is what we did in the <common> section.
You can also read more on configuring commons logging to use log4net from the Common.Logging documentation.

3 comments:

Alex Brown said...

When I try this, I get an error:
http://stackoverflow.com/questions/2236772/quartz-net-and-common-logging-using-log4net

J said...

I think you have a version conflict. Try redirecting the common.logging assembly as detailed here: http://netcommon.sourceforge.net/docs/2.0.0/reference/html/ch01.html#d4e45

Anonymous said...

Hello
Can you please tell me how to load an external log4net configuration file
Thanks