Logging to CouchDB from C#

Setting up a centralized form of logging can be a pain. CouchDB can help make it easy.


First, grab the log4net library. It's common, proven, and makes logging straightforward.

Second, pull down PostLog from github and build

$ git clone https://statianzo@github.com/statianzo/PostLog.git
$ cd PostLog
$ msbuild PostLog.sln

PostLog is an HttpAppender for log4net. It will make an HTTP request for logging events.

Finally, with an instance of CouchDB running, create a database for logging against.

$ curl -X PUT http://localhost:5984/testlog

Use log4net like normal

If your project currently uses, you're in business. If not, take the following example:

using System;
using log4net.Config;
using log4net;

namespace CouchDBAppenderExample
  class MainClass
    public static void Main (string[] args)
      ILog logger = LogManager.GetLogger(typeof(MainClass));
      Console.WriteLine ("Press enter to do it");
      try {
        throw new InvalidOperationException("Don't do that!");
      catch(InvalidOperationException e) {
        logger.Error("It blew up", e);

A typical log4net usage. An exception occurs and it gets logged.

Configure log4net

PostLog's HttpAppender is configured in the log4net app.config section. Several options are available.

A sample configuration is as follows:

<?xml version="1.0"?>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

    <appender name="HttpAppender" type="PostLog.HttpAppender, PostLog">
      <uri value="http://localhost:5984/testlog" />
      <formatterType value="PostLog.JsonBodyFormatter, PostLog" />
        <name value="date" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
        <name value="level" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        <name value="message" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        <name value="exception" />
        <layout type="log4net.Layout.ExceptionLayout" />
      <level value="DEBUG" />
      <appender-ref ref="HttpAppender" />

With the new appender in place, your log statements should start hitting CouchDB.


Now that your log statements are in Couch, you probably want to look at them.

Let's create a file named logging.json. The file has a view to see all error log statements.

  "language": "javascript",
    "error": {
      "map": "function(doc) { if (doc.level === 'ERROR')  emit(doc.date, doc) }"

Send it to Couch with curl:

$ curl -T - http://localhost:5984/testlog/_design/logging < logging.json

Now we can view all error statements by calling

$ curl http://localhost:5984/testlog/_design/logging/_view/error


An example project is available on github at: https://github.com/statianzo/CouchDBAppenderExample

14 May 2011