Separating out Enterprise Library V5 files

This is a just a short blog on how to keep those app.config files in order. Have you noticed just how big the app.config file becomes in a large project? Did you ever have trouble trying to find something within this horrendously large file? Well this should not happen when using the Enterprise Library V5 as the entries for each of the Application Blocks can reside in its own file. Does this sound too good to be true? Read on…..

Here is a simple EntLib V5 setup (the actual details within each Application Block are unimportant as we are only dealing with structure here).

Now add the following solution directories to your application similar to what is shown here:

Now add config files for each Application Block section defined in your App.Config so that the project structure looks like the following:

Okay, now back to the Configurator application as it is time to setup the Configuration Sources. In this exercise the Configuration Sources are going to be file based configuration files. The following screen shows how this is done.

Now after adding the Configuration Source and the Exception Configuration Source we have the following in the Configurator:

The resulting XML in the app.config file is:

<?xml version=1.0 encoding=utf-8 ?>
<configuration>
    <configSections>
        <section name=validation type=Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, 
                                         Microsoft.Practices.EnterpriseLibrary.Validation, 
                                         Version=5.0.414.0, 
                                         Culture=neutral, 
                                         PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
        <section name=loggingConfiguration type=Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, 
                                                   Microsoft.Practices.EnterpriseLibrary.Logging, 
                                                   Version=5.0.414.0, 
                                                   Culture=neutral, 
                                                   PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
        <section name=exceptionHandling type=Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, 
                                                Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, 
                                                Version=5.0.414.0, 
                                                Culture=neutral, 
                                                PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
        <section name=enterpriseLibrary.ConfigurationSource type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, 
                                                                    Microsoft.Practices.EnterpriseLibrary.Common, 
                                                                    Version=5.0.414.0, 
                                                                    Culture=neutral, 
                                                                    PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
    </configSections>
  


    <validation />
    <loggingConfiguration name=“” tracingEnabled=true defaultCategory=General>
        <listeners>
            <add name=Event Log Listener 
                 type=Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, 
                       Microsoft.Practices.EnterpriseLibrary.Logging, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                 listenerDataType=Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, 
                                   Microsoft.Practices.EnterpriseLibrary.Logging, 
                                   Version=5.0.414.0, 
                                   Culture=neutral, 
                                   PublicKeyToken=31bf3856ad364e35

                 source=Enterprise Library Logging 
                 formatter=Text Formatter
                 log=“” 
                 machineName=. 
                 traceOutputOptions=None />
        </listeners>
        <formatters>
            <add type=Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, 
                       Microsoft.Practices.EnterpriseLibrary.Logging, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                template=Timestamp: {timestamp}{newline} Message: {message}{newline} Category: {category}{newline}
                          Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline}
                          Title:{title}{newline} Machine: {localMachine}{newline} App Domain: {localAppDomain}{newline}
                          ProcessId: {localProcessId}{newline} Process Name: {localProcessName}{newline}
                          Thread Name: {threadName}{newline} Win32 ThreadId:{win32ThreadId}{newline}
                          Extended Properties: {dictionary({key} – {value}{newline})}

                name=Text Formatter />
        </formatters>
        <categorySources>
            <add switchValue=All name=General>
                <listeners>
                    <add name=Event Log Listener />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue=All name=All Events />
            <notProcessed switchValue=All name=Unprocessed Category />
            <errors switchValue=All name=Logging Errors &amp; Warnings>
                <listeners>
                    <add name=Event Log Listener />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <exceptionHandling>
        <exceptionPolicies>
            <add name=Policy>
                <exceptionTypes>
                    <add name=All Exceptions type=System.Exception, 
                                                     mscorlib, Version=4.0.0.0, 
                                                     Culture=neutral, 
                                                     PublicKeyToken=b77a5c561934e089

                        postHandlingAction=NotifyRethrow />
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
  
    <enterpriseLibrary.ConfigurationSource selectedSource=System Configuration Source>
        <sources>
            <add name=System Configuration Source 
                 type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, 
                       Microsoft.Practices.EnterpriseLibrary.Common, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35 />
            <add name=Exception Configuration Source 
                 type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, 
                       Microsoft.Practices.EnterpriseLibrary.Common, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                 filePath=.\Config\EntLibV5\Exception.config />
        </sources>
        <redirectSections>
            <add sourceName=Exception Configuration Source name=exceptionHandling />
        </redirectSections>
    </enterpriseLibrary.ConfigurationSource>
</configuration>

This really seems considerable – however just wait until all the exception processing and validation rules are added for the complete application. The next task requires that the ConfigSections be taken out and placed in their respective sub-config files. The designer view is:

The final XML is then as follows:

<?xml version=1.0 encoding=utf-8 ?>
<configuration>
    <configSections>
        <section name=enterpriseLibrary.ConfigurationSource type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, 
                                                                    Microsoft.Practices.EnterpriseLibrary.Common, 
                                                                    Version=5.0.414.0, 
                                                                    Culture=neutral, 
                                                                    PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
    </configSections>
  

    <enterpriseLibrary.ConfigurationSource selectedSource=System Configuration Source>
        <sources>
            <add name=System Configuration Source 
                 type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, 
                       Microsoft.Practices.EnterpriseLibrary.Common, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35 />
            <add name=Exception Configuration Source 
                 type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, 
                       Microsoft.Practices.EnterpriseLibrary.Common, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                 filePath=.\Config\EntLibV5\Exception.config />
            <add name=Logging Configuration Source 
                 type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, 
                       Microsoft.Practices.EnterpriseLibrary.Common, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                 filePath=.\Config\EntLibV5\Logging.config />
            <add name=Validation Configuration Source 
                 type=Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, 
                       Microsoft.Practices.EnterpriseLibrary.Common, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                 filePath=.\Config\EntLibV5\Validation.config />
        </sources>
        <redirectSections>
            <add sourceName=Exception Configuration Source name=exceptionHandling />
            <add sourceName=Validation Configuration Source name=validationHandling />
            <add sourceName=Logging Configuration Source name=loggingHandling />
        </redirectSections>
    </enterpriseLibrary.ConfigurationSource>
</configuration>

The Logging application config file Logging.config now looks like:

<?xml version=1.0 encoding=utf-8 ?>
<configuration>
    <configSections>
        <section name=loggingConfiguration type=Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, 
                                                   Microsoft.Practices.EnterpriseLibrary.Logging, 
                                                   Version=5.0.414.0, 
                                                   Culture=neutral, 
                                                   PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
    </configSections>

    <loggingConfiguration name=“” tracingEnabled=true defaultCategory=General>
        <listeners>
            <add name=Event Log Listener 
                 type=Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, 
                       Microsoft.Practices.EnterpriseLibrary.Logging, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                 listenerDataType=Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, 
                                   Microsoft.Practices.EnterpriseLibrary.Logging, 
                                   Version=5.0.414.0, 
                                   Culture=neutral, 
                                   PublicKeyToken=31bf3856ad364e35

                 source=Enterprise Library Logging 
                 formatter=Text Formatter
                 log=“” 
                 machineName=. 
                 traceOutputOptions=None />
        </listeners>
        <formatters>
            <add type=Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, 
                       Microsoft.Practices.EnterpriseLibrary.Logging, 
                       Version=5.0.414.0, 
                       Culture=neutral, 
                       PublicKeyToken=31bf3856ad364e35

                template=Timestamp: {timestamp}{newline} Message: {message}{newline} Category: {category}{newline}
                          Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline}
                          Title:{title}{newline} Machine: {localMachine}{newline} App Domain: {localAppDomain}{newline}
                          ProcessId: {localProcessId}{newline} Process Name: {localProcessName}{newline}
                          Thread Name: {threadName}{newline} Win32 ThreadId:{win32ThreadId}{newline}
                          Extended Properties: {dictionary({key} – {value}{newline})}

                name=Text Formatter />
        </formatters>
        <categorySources>
            <add switchValue=All name=General>
                <listeners>
                    <add name=Event Log Listener />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue=All name=All Events />
            <notProcessed switchValue=All name=Unprocessed Category />
            <errors switchValue=All name=Logging Errors &amp; Warnings>
                <listeners>
                    <add name=Event Log Listener />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

The Exception.config is:

<?xml version=1.0 encoding=utf-8 ?>
<configuration>
    <configSections>
        <section name=exceptionHandling type=Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, 
                                                Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, 
                                                Version=5.0.414.0, 
                                                Culture=neutral, 
                                                PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />

    </configSections>

    <exceptionHandling>
        <exceptionPolicies>
            <add name=Policy>
                <exceptionTypes>
                    <add name=All Exceptions type=System.Exception, 
                                                     mscorlib, Version=4.0.0.0, 
                                                     Culture=neutral, 
                                                     PublicKeyToken=b77a5c561934e089

                        postHandlingAction=NotifyRethrow />
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>

And the Validation.config is:

<?xml version=1.0 encoding=utf-8 ?>
<configuration>
    <configSections>
        <section name=validation type=Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, 
                                         Microsoft.Practices.EnterpriseLibrary.Validation, 
                                         Version=5.0.414.0, 
                                         Culture=neutral, 
                                         PublicKeyToken=31bf3856ad364e35 
                 requirePermission=true />
    </configSections>
    <validation />
</configuration>

As can be seen the ability to separate out the Application Blocks from the App.config file is a great help when trying to maintain a large application. This separation can take place at any time during the project as long as the above guideline is followed.

All that is left is to select these sub-files using the Configurator and start entering the configuration data graphically.

That’s all folks…

Advertisements

~ by Intelligence4 on January 14, 2011.

 
%d bloggers like this: