Enabling Logging In Your Agatha Service Layer

2 commentsWritten on December 19th, 2009 by
Categories: agatha

As of Agatha 1.0 beta 2, you can now use whatever logging library you prefer.  Agatha now uses the Common.Logging project instead of using a logging library directly.  This means you just need to add a bit of configuration to your service host to enable logging and get that logging information in whatever format or manner you want.  Enabling this is pretty easy, since you just need to follow the standard approaches for configuring Common.Logging, and obviously also for the logging library that you use.  I’m just going to show a short example of using XML in your web.config (or app.config depending on your service host) to get this working.

First, you need to add the configuration section for Common.Logging to the <configSections> element:

    <sectionGroup name="common">

      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />

    </sectionGroup>

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>

 

As you can see, i also included the definition of my preferred logging library (log4net) in this as well.  In this particular case, your service host needs to reference the following assemblies: Common.Logging.dll, Common.Logging.Log4Net.dll and log4net.dll.

The configuration of the Common.Logging library looks like this:

  <common>

    <logging>

      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">

        <arg key="configType" value="INLINE" />

      </factoryAdapter>

    </logging>

  </common>

 

You need to specify which factoryAdapter that Common.Logging will use, in our case the one for log4net.  If you then configure the INLINE value for the configType key, Common.Logging will just initialize log4net in a way that it will simply use the XML configuration that is also present in your config file.  Again, i’ll point you to the Common.Logging documentation for configuring it for your specific library if you’re using something else. 

In my case, my log4net configuration looks like this:

  <log4net>

    <logger name="Agatha">

      <level value="ERROR"/>

      <appender-ref ref="LogFileAppender" />

    </logger>

 

    <logger name="AgathaPerformance">

      <level value="WARNING"/>

      <appender-ref ref="LogFileAppender" />

    </logger>

 

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >

      <param name="File" value="log.txt" />

      <param name="AppendToFile" value="true" />

      <rollingStyle value="Size" />

      <maxSizeRollBackups value="10" />

      <maximumFileSize value="10MB" />

      <staticLogFileName value="true" />

 

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} - %m%n" />

      </layout>

    </appender>

  </log4net>

 

The ‘Agatha’ logger simply logs everything coming from types whose namespace starts with Agatha and that logs at the error level (basically just exceptions caught by the Request Processor) and sends that the LogFileAppender (which simply logs in a log.txt) file.  The ‘AgathaPerformance’ logger specifically logs performance warnings coming from Agatha’s PerformanceLoggingRequestProcessor and also sends it to the LogFileAppender.

And that’s all there is to it.

  • bennyb

    How will you use Agatha with ELMA?

    • http://davybrion.com Davy Brion

      no idea, never used ELMAH…

      but Agatha uses Common.Logging, so you just need a Common.Logging adapter for your logging framework… i know there are adapters for log4net and enterprise library, so i guess there’s one for ELMAH as well, and if not, it shouldn’t be hard to implement