How can I change the name of entlib's logging configuration section?

Topics: Logging Application Block
Aug 7, 2013 at 11:00 AM
Edited Aug 7, 2013 at 11:01 AM
Hi,

I don't like the default name of entlib's logging configuration section (loggingConfiguration), because it is not aligned to other sections' names (e.g., neither exceptionHandling nor unity have "Configuration" in their names).

I've tried to change it by changing the "section" tag in "configSections" and the "loggingConfiguration" tag, but it doesn't work. I specifically want it to be named "logging".

Does anyone know how to do it?

Thank you very much.
Aug 7, 2013 at 10:35 PM
By default Enterprise Library looks for the predefined section names. If you want to use different names then you will have to perform a bit of manual configuration.

Here's how to do it using code for Enterprise Library 5:
var logSettings = ConfigurationManager.GetSection("logging") as LoggingSettings;

var builder = new ConfigurationSourceBuilder()
    .AddSection(LoggingSettings.SectionName, logSettings);

var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
            
EnterpriseLibraryContainer.Current
    = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

The above only configures logging. So, if you have other sections (e.g. exceptionHandling) they will also need to be manually configured or you could use a slightly different approach to load all default configuration and then set up the sections that have "overridden" names:
// Create the container
IUnityContainer container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();

// Configurator will read Enterprise Library configuration 
// and set up the container
UnityContainerConfigurator configurator = new UnityContainerConfigurator(container);

// Map logging to loggingConfiguration
var logSettings = ConfigurationManager.GetSection("logging") as LoggingSettings;
var configSource = new DictionaryConfigurationSource();
configSource.Add(LoggingSettings.SectionName, logSettings);

EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);

EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

// ... Resolve LogWriter as well as other Enterprise Library objects
LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
  
Another option would be to change Enterprise Library to use the exact section names you want.


I'll also add that if you are using Enterprise Library 6 it's much more straight forward:
var factory = new LogWriterFactory((name) =>
    {
        return (ConfigurationSection)ConfigurationManager.GetSection("logging");
    }
);

Logger.SetLogWriter(factory.Create());

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to