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
If your project currently uses, you’re in business. If not, take the following example:
A typical log4net usage. An exception occurs and it gets logged.
PostLog’s HttpAppender
is configured in the log4net
app.config section.
Several options are available.
uri
The location to send the HTTP Requestmethod
The HTTP method to be used (defaults to POST)useragent
The value of the useragent headerformat
The format to serialize the log data. Options are form, json, and xmlxmlrootname
root element tag for xmlA sample configuration is as follows:
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.
{
"_id":"_design/logging",
"language": "javascript",
"views":
{
"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