Sdílet prostřednictvím


"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!

Comments

  • Anonymous
    November 30, 2010
    Hello - I know this is an old post, but maybe you can help. I'm trying to use this in a WPF app - your technique does sidetrack the configuration exceptions, but I can't find the trace.log that i'm supposedly writing to. Do you have a sample MyLoggingConfiguration.config?

  • Anonymous
    April 14, 2011
    Have you tried looking at Event Log through event viewer.