Monday, July 14, 2008

Read AppSettings Section from MyApp.dll.config

Its been long time, when I was struggling to read DLL Configuration file settings, and finally I found the solution. My app.config content is as :


<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler"/>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ><section name="Lab3.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="C:\\AppConfig\\Log.txt"/>
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Composite"/>
<param name="DatePattern" value="yyyyMMdd"/>
<param name="MaxSizeRollBackups" value="10"/>
<param name="MaximumFileSize" value="50KB"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %-5p %c [%x] - %m%n"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
<appSettings>
<add key="UserName" value="MyUserName"/>
</appSettings>
<applicationSettings>
<MyApp.Properties.Settings>
<setting name="MyApp_SampleWS_Service1" serializeAs="String">
<value>http://localhost/SampleWebService/Service1.asmx</value>
</setting>
</Lab3.Properties.Settings>
</applicationSettings>
</configuration>


The assembly (MyApp.dll) is deployed in GAC, and the once compiled, I copied MyApp.dll.config from build folder to C:\AppConfig. This path is hardcoded in the code so that it can be picked up easily by the code.
This app.config has three major parts:
1. Log4Net - to write log details in a text file.
2. AppSettings
3. WebService Reference.

The piece of code that reads this config file from C:\AppConfig location:


ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"C:\AppConfig" + @"\MyApp.dll.config";
if (!File.Exists(fileMap.ExeConfigFilename))
{
logger.Info("File " + fileMap.ExeConfigFilename + " does not found.");
return;
}
assemblyConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection appSettings = assemblyConfig.AppSettings;
logger.Info("App Settings found");
logger.Info("Key=UserName, Value=" + appSettings.Settings["UserName"].Value);
logger.Info("End of Information");

And two static variable declarations:

private static ILog logger = LogManager.GetLogger("Atul.Sample.ConfigSample");
private static Configuration assemblyConfig;

Add following line of code in AssemblyInfo.cs too to make log4net working correctly.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"C:\AppConfig\MyApp.dll.config", Watch = true)]

Once again, make sure to copy 'MyApp.dll.config' in the C:\AppConfig location to make it work correctly.

Assumption is that your assembly is 'strongly named' as this is required for deployment in GAC.

No comments: