How to extract custom section values from custom configuration file programatically?

Topics: Building and extending application blocks, Data Access Application Block, Enterprise Library Core, General discussion, Logging Application Block
Nov 14, 2008 at 2:33 PM
Edited Nov 14, 2008 at 2:35 PM

Hello All,

I have created on custom configurartion file as below : 

<

configuration>
    <
configSections>
        <
section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />  

 

    </

configSections>  

 

 

<

loggingConfiguration name="Logging Application Block" tracingEnabled="truedefaultCategory="" logWarningsWhenNoCategoriesMatch="true">

 

 

 

<

 

 

listeners>

 

<

 

Dev>

 

<

 

add fileName="C:\Documents and Settings\kaupatel\Sample Applications\tracedev.log"

 

 

 

 

 

 

header="----------------------------------------" footer="----------------------------------------"

 

 

 

 

 

 

formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="FlatFile TraceListener" />

 

</

 

Dev>

 

<

 

Stage>

 

<

 

add fileName="C:\Documents and Settings\kaupatel\Sample Applications\tracestage.log"

 

 

 

 

 

 

header="----------------------------------------" footer="----------------------------------------"

 

 

 

 

 

 

formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="FlatFile TraceListener" />

 

</

 

Stage>

 

<

 

Prod>

 

<

 

add fileName="C:\Documents and Settings\kaupatel\Sample Applications\traceprod.log"

 

 

 

 

 

 

header="----------------------------------------" footer="----------------------------------------"

 

 

 

 

 

 

formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="FlatFile TraceListener" />

 

</

 

Prod>

 

</

 

listeners>

 

<

 

formatters>

 

<

 

Dev>

 

<

 

add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"

 

 

 

 

 

 

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Text Formatter" />

 

</

 

Dev>

 

<

 

Stage>

 

<

 

add template="Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"

 

 

 

 

 

 

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Text Formatter" />

 

</

 

Stage>

 

<

 

Prod>

 

<

 

add template="Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"

 

 

 

 

 

 

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Text Formatter" />

 

</

 

Prod>

 

</

 

formatters>

 

 

<

listeners>

 

<

 

add name="FlatFile TraceListener" />

 

</

 

listeners>

 

</

 

errors>

 

</

 

specialSources>

 

</

 

loggingConfiguration>

 

</

 

configuration>

In above configuration file, i have provided section as 'Dev','Stage','Prod'.
Now I want to get logging configuration details depends on the environment programatically in some wrapper class.
I have alerady set environment in web.config. So, If i set envirnment as 'Dev', Template used while adding configuration details must be as per the environment.
It means it must used template which defined under DEV tag not in 'Stage' & 'Prod' Tag.
I want to do this functionality programatically in some wrapper class.

Can someone guide me how to create wrappre class for this & which namespace i have to refer?
If some one has created sample application for this type of functionality then please..please ..please do provide some sample code to load configuration details programatically in some wrapper class.

Thanks,
Kaushal.

 

 

Nov 17, 2008 at 7:59 AM

Hi Kaushal,

First, I would like to ask how did you created your config file, did you manually add the <dev>, <stage> and <prod> tags?

The only way I can think of is to modify the config file programatically during runtime by using the ConfigurationManager of the System.Configuration namespace. see this link:

http://msdn.microsoft.com/en-us/library/cc511916.aspx 




Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 17, 2008 at 9:15 AM
Yes,
I have manually added <dev>,<stage>,<prod> tag.

Basically, my requirement is as below.
I have created one configuration file using Microsoft Enterprise Library editor for logging (and database).
Now, I want to add above tag <dev>,<stage>,<prod> in that configuration file for environment based configuration in such a way that it load configuration depends on environment.

Is there any way through which i can create/register some custom section in config file OR do i need to make changes LogSettings.cs/GetLoggingSettings() any other method?
As Enterprise Library is open source, i am looking for the code change in Enterprise Library configuration codebase itself.

Do you have any idea, where should i make change in codebase?

Thanks,
Kaushal
Feb 10, 2009 at 8:50 PM

See this:

http://msdn.microsoft.com/en-us/library/dd139945.aspx

on the use of environmental overrides.