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://email@example.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.
HttpAppender is configured in the
log4net app.config section.
Several options are available.
uriThe location to send the HTTP Request
methodThe HTTP method to be used (defaults to POST)
useragentThe value of the useragent header
formatThe format to serialize the log data. Options are form, json, and xml
xmlrootnameroot element tag for xml
A 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.
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