Skip to main content

Quick Log4net setup

Log4net has lots of options and different ways it could be configured - these are notes written to myself so that I can get it working quickly.

1. Download log4net

Download from here.

2. Drop DLL in Bin folder

In the download package, there are various DLLs, usually you'll want the .net 2.0 release version at:
If you have an ASP.NET Web Application Project (as opposed to an ASP.NET Web Site) then you'll also need to add a reference to the DLL.

3. Add a Configurator line in the Global.asax

There are various ways of telling Log4net to pick up its config. We're going to use the Xml Configurator that pulls config from a separate config file. Add this line of code to Application_Start in the Global.asax:
void Application_Start(object sender, EventArgs e) 
4. Add an AppSetting to the web.config

The XmlConfigurator.Configure() call above, tells Log4net to look for a certain key in appSettings. The key gives the name of the config file. So we add the correct appSetting entry to web.config, like this:
    <add key="log4net.Config" value="logging.config" />
5. Add a logging.config file

In the root folder (same folder as web.config), add the following logging.config file:
<?xml version="1.0" encoding="utf-8" ?>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="App_Data/Log.txt" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d thread%-2t %-5p src:%-25c - %m%n" />

      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
This has basic options for logging to a text file in the App_Data folder (make sure you have an App_Data folder). Its set to log everything, because during development thats what you'll want to do, right?

6. Add logging statements

One good pattern for this is to have a static logger for each page:
public partial class MyWebPage : System.Web.UI.Page
    private static readonly ILog logger = LogManager.GetLogger(typeof(MyWebPage));
    protected void Page_Load(object sender, EventArgs e)
        logger.Debug("page load");

So ... thats one way of doing it. There are, of course, millions of other ways.


Popular posts from this blog

SSRS multi-value parameters with less fail

SSRS supports multi-value parameters, which is nice, but there are a few issues with them. This is how I deal with them. Two of the problems with SSRS multi-value parameters are: You have to jump through a few hoops to get them to work with stored procedures The (Select All) option, as shown above The reason the (Select All) option is a problem is that it is a really inelegant way of saying 'this parameter does not matter to me'. If you have a list with hundreds of values, passing all of them as a default option just seems wrong. Also, if your report shows the user which items they selected, printing the whole list when they choose (Select All) is excessive. So in this post I'm going to show my particular way of: Jumping through the hoops to get Multi-Value params in a stored procedure Adding a single '--All--' value that the report interprets as meaning all the options. Getting Multi-Value params to work with Stored Procedures This is

Copying data to Salesforce Sandboxes using TalenD

A common problem with Salesforce Developer Sandboxes is that they are blank. Really you're going to want some data in there, so there are various strategies for copying data from your live instance to the Sandbox. There are some paid-for solutions - SFXOrgData , Salesforce Partial Data Sandboxes - but if you've got a decent ETL tool you can build your own. There are a bunch of free ETL tools for Salesforce: JitterBit Data Loader is good for quick ad-hoc tasks but the free version makes it difficult to manage specific ETL projects or share projects with other users Pentaho Community Edition - an open source edition of the enterprise version Apatar was a free open source Salesforce ETL which still works but development seems to have stopped since 2011 TalenD Open Studio is an open source ETL tool For the task of copying data from live to a Sandbox, either Pentaho or TalenD Open Studio could be used, depending on preference. Here's a good comparison of the dif

Bug Hunter in Space

In 1987, Acorn launched the Archimedes home computer. At the time, it was the fastest desktop computer in the world, and at the time, I was fortunate enough to have one to experiment with. The Archimedes was great, but it never really took off commercially. However, it was built around the ARM processor, which Acorn had designed itself when it could not find any existing processors suitable for its 32-bit ambitions. The ARM processor was a masterpiece of simple and intuitive design, and its still around today, with most of the instruction set pretty much unchanged. In fact, you've probably got one in your pocket right now. Its design makes it process very efficiently on low energy intake, and hence it is estimated that about 98% of all mobile phones contain an ARM chip. Over 10 billion ARM chips have been shipped, and they outnumber Intel's long running x86 series of chips by a factor of about 5 to 10. I had learned programming on the BBC Model B , and when we got the A