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.
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:
<appender name="Console" type="log4net.Appender.ConsoleAppender">
%-9timestamp %method,%line[%thread] %message%newline"/>
Write one line to
Main method to log some debug information out to console:
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:
(ConfigFile = "log4net.config", Watch = true)]
Start the application. You should see logged line on the console window:
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:
value="%-5level %-9timestamp %method,%line[%thread] %message%newline"/>
<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. :)