Click here to Skip to main content
Click here to Skip to main content

log4net for multiple build configurations

, 18 Jul 2014 CPOL
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

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 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 purpose. 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 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" ?>
<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:

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="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:

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 Smile | :)

License

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

Share

About the Author

Ryszard Dżegan
Software Developer Rule Financial
Poland Poland
C# Developer.
Follow on   Twitter

Comments and Discussions

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