Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

log4net for Multiple Build Configurations

5.00/5 (5 votes)
18 Jul 2014CPOL3 min read 26.9K  
How to prepare log4net or other custom, XML-based configuration file for particular build or environment

Introduction

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:

Image 1

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

Image 2

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

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

C#
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:

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

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

Image 3

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:

Image 4

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

Image 5

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

Image 6

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

XML
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appender name="ColoredConsole" 
  type="log4net.Appender.ColoredConsoleAppender" xdt:Transform="Replace">
    <mapping>
      <level value="DEBUG"/>
      <foreColor value="Green"/>
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern 
      value="%-5level %-9timestamp %method,%line[%thread] %message%newline"/>
    </layout>
  </appender>
  <root>
    <level value="ALL"/>
    <appender-ref ref="ColoredConsole" xdt:Transform="Replace"/>
  </root>
</log4net>

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

Image 7

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

Image 8

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:

Image 9

Let's check Release configuration for sure:

Image 10

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

Image 11

Thank you for reading. :)

License

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