Wednesday, March 12, 2008

Log4Net tips

  • If you want to store connection string in one place in the web.config, you can remove the connectionString node under AdoNetAppender. And set the ConnectionString property of the log4net.Appender.ADONetAppender at code behind. You need to make it a function and then put this function in the Application_Start of the global.asax.vb file. So next time instead of calling log4net.Config.DOMConfigurator.Configure(), you can call this function. Here is how you can do it.
  • If you want to add more columns to the log table, say the client's IP address. You will need create the column in the log table. And then modified the commandText node to insert the IP address to the log table under AdoNetAppender in the web.config. Also need to add the parameter node for the IP address. Then use the MDC.Set(...) function to set the IP address in the code behind before call the logger.Error(...) function. Here is an good example.
blog comments powered by Disqus