"The configuration section for Logging cannot be found in the configuration source." or how to read an external configuration file for the Logging Application Block

When using the Logging Application Block, using an IConfigurationSource to read your logging configuration from an external reference might throw a ConfigurationErrorsException("The configuration section for Logging cannot be found in the configuration source."), or ignore your configuration changes. This exception is usually caused by the Logger façade, which forcibly initializes using the application default .NET configuration file and not the one specified in the IConfigurationSource. In this scenario, avoid using the Logger class and use the LogWriter created from the Iconfiguration source.

// Reads a Logging Application Block configuration from an external configuration file

var fcs = new FileConfigurationSource(@"C:\TEMP\MyLoggingConfiguration.config");

var logWriter = new LogWriterFactory(fcs).Create();

var logEntry = new LogEntry() { Message = "Hello logging!" };

// You can use the logWriter directly

logWriter.Write(logEntry);

// Throws a ConfigurationErrorsException "The configuration section for Logging cannot be found in the configuration source."

// Because the Logger facade forcibly initializs using the application’s default .NET configuration file and not the one
// we specified manually (EntLib 4.1)

Logger.Write(logEntry);

Thanks to Renaud providing me an idea for the post!