Click here to Skip to main content
13,251,998 members (61,263 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 18 Jul 2014

log4net for Multiple Build Configurations

, 18 Jul 2014
Rate this:
Please Sign up or sign in to vote.
How to prepare log4net or other custom, XML-based configuration file for particular build or environment


In enterprise solutions, there is a need to keep several build configurations for different environments like Dev, QA, UAT and Prod. Each of which is likely to have its own configuration file that covers specific database connection, different application settings and so on. Ideally developer should have the possibility to only select particular build option in Visual Studio to get relevant configuration file generated to him. There are few strategies to make that done. One of them is to use Configuration Transformation. In such approach, we have one general configuration file App.config or Web.config which has child configurations for each build. Each child contains some specific additional elements or replacements that are combined with general file while project is being built under particular configuration like DEBUG, Dev, QA, etc.

You don't need to be familiarized with transformation concept for further reading. My example will be simple and self-explanatory.

The one problem that I have encountered while working with transformations so far is that Visual Studio has poor support for them. In this tip, I'm going to show how to use easy and seamless configuration transformation for log4net (which often has its own config file) by using Slow Cheetah tool.

Solution Preparation

We need a simple console application in Visual Studio for demo purposes. Step two is to install log4net. I will install it using Package Manager Console:

After installation, packages.config file is added to the project:

Now, we could add log4net configuration to App.config file, but we will create separate log4net.config file because we want there to be more Separation of Concerns in our project. Let's set ConsoleAppender in order to get logs in Console Window:

<?xml version="1.0" encoding="utf-8" ?>
  <appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5level 
      %-9timestamp %method,%line[%thread] %message%newline"/>
    <level value="ALL"/>
    <appender-ref ref="Console"/>

Write one line to Main method to log some debug information out to console:

using log4net;
using log4net.Config;

namespace Test
    class Program
        static ILog Log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
            Log.Debug("Log debug message");

Remember to set breakpoint after this line in order to stop debugger and see the log output on console window while debugging.

There is one thing left. We need to instruct log4net where its configuration is. We can issue this in code or declare it on assembly level. I will use the latter option. Append the following line to AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator
(ConfigFile = "log4net.config", Watch = true)]

Start the application. You should see logged line on the console window:

Fork Configuration

Let's say that for production environment black&white logging is sufficient but for debug purposes we would like to have something more fancy like ColoredConsoleAppender. We will split log4net configuration to achieve our goal. It's time for Slow Cheetah now. This time I will use Tools/Extensions and Updates... menu option. Search for Slow Cheetah and download it:

After downloading, installing and restarting Visual Studio, click right on log4net file. Notice Add Transform item and click it:

Wait few seconds. Accept all two message boxes. The final results are children log4net.Debug.config and log4net.Release.config:

We're ready to implement ColoredConsoleAppender in for Debug build configuration. Please edit log4net.Debug.config file as follows:

<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns:xdt="">
  <appender name="ColoredConsole" 

  type="log4net.Appender.ColoredConsoleAppender" xdt:Transform="Replace">
      <level value="DEBUG"/>
      <foreColor value="Green"/>
    <layout type="log4net.Layout.PatternLayout">

      value="%-5level %-9timestamp %method,%line[%thread] %message%newline"/>
    <level value="ALL"/>
    <appender-ref ref="ColoredConsole" xdt:Transform="Replace"/>

Slow Cheetah allows developer to check the result of the transformation. Click right on log4net.Debug.config and notice an option Preview Transform:

When you click on this option, a nice diff window will appear:

Let's start the application on current build configuration which should be Debug and notice that now the log output is green as we wanted. It means, that Debug build has used its special log4net configuration:

Let's check Release configuration for sure:

The output is white as originally since we haven't altered log4net configuration for Release build:

Thank you for reading. :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ryszard Dżegan
Software Developer GFT
Poland Poland
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 18 Jul 2014
Article Copyright 2014 by Ryszard Dżegan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid