Friday, April 20, 2007

AdoNetAppender in log4net

I've been running tests recently that required a quite detailed look into log files that log4net generates as part of our logging framework. Currently we have a number of Appenders loaded, such as RollingFileAppender, to log to different places.

However, a text file isn't always that easy to search through. Finding 1 separate entry is fine, but you can't list all of the error logs you have in the file, and you can't just show all messages that contain a particular string or string pattern. You could do this if these log entries were in a database table.

So as a little test I loaded in an AdoNetAppender after setting up a test database table to log to. I just used the config example as a base, and started logging.

That was all I really needed to do, I was impressed with how easy it was. It simplified the searches that I needed to do no end, and it would be a pretty easy task to tack an ASP.Net front end onto it to work as a log reader.

The Appender is very flexible, only taking a SQL statement to execute and a number of parameters to pass into it. The example config entry gives you pretty much everything you need, except maybe the machine name where the log entry came from.

If you do need this then it is relatively easy to add, due to the Appender's flexibility. Just add a column to the database (I've called it MachineName), and change the SQL statement to pass in a @machineName parameter.

Then in the parameters list, put something like this:

  <parameterName value="@machineName" />
  <dbType value="String" />
  <size value="50" />
  <layout type="log4net.Layout.PatternLayout">
            value="%property{log4net:HostName}" />

That's all you need to log the machine name where the log originated.

1 comment:

Anonymous said...

can u explain it in more detail... with showing class files and config file...