.Net basics: using configuration file to configure logging and database connection string

Hello everybody,

Today I will cover next topics – How to configure logger and database connection string in .Net Framework X.X via xml configuration file.

To add logging into your application you have to do few simple steps:
1. Add a configuration file into your project: Project -> Add new Item -> Application configuration file or CTRL+SHIFT+A -> Application configuration file;
2. Open the file you have added;
— This file is contained in your project’s tree and will be added into the Debug and Release folders after you build the project automatically;
— File will be automatically renamed to YOUR_APP.exe.config;
— You can add any configuration information into this file;
— Recently added file looks like:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
</configuration>

— You are free to modify it to fit your needs, but using predefined sections, for example you can put any data into the section <appSettings></appSettings> or you can put database connection strings into the special section:
<connectionStrings>
<add name=”MSSQL2005:TESTDATABASE”
connectionString=”Data Source=YAHORDESKTOPSQLEXPRESS;Initial Catalog=testdatabase;Integrated Security=True;Pooling=true;Min Pool Size=14;”
providerName=”System.Data.SqlClient” />
</connectionStrings>

3. Add configuration section for System.Diagnostics package:

<system.diagnostics>
<trace autoflush=”true” indentsize=”0″>
<listeners>
<add name=”LogListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”.myApp.log”/>
</listeners>
</trace>
</system.diagnostics>

3.1. To add random config section use appSettings:

<appSettings>
<add key=”send_port” value=”8383″ />
</appSettings>

3.2. So your configuration file will look like:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>

<appSettings>
<add key=”send_port” value=”8383″ />
</appSettings>

<connectionStrings>
<add name=”MSSQL2005:TESTDATABASE”
connectionString=”Data Source=YAHORDESKTOPSQLEXPRESS;InitialCatalog=testdatabase;
Integrated Security=True;Pooling=true;Min Pool Size=14;”
providerName=”System.Data.SqlClient” />
</connectionStrings>

<system.diagnostics>
<trace autoflush=”true” indentsize=”0″>
<listeners>
<add name=”LogListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”.myApp.log”/>
</listeners>
</trace>
</system.diagnostics>

</configuration>

4. Into your source code add using:

using System.Diagnostics;

It is enough to use logging into the myApp.log file (implemented asynchronously) by calling appropriate static methods from the Trace class.
For example: Trace.WriteLine(“Hello logging”);

5. Using configuration file;
5.1. To access its keys you need to add Reference to System.Configuration library and add following using:

using System.Configuration;

5.2. ConfigurationManager’s class static methods to access your settings, stored into the file

string sPort = ConfigurationManager.AppSettings[“send_port”];

string connectionString =ConfigurationManager.ConnectionStrings[“MSSQL2005:TESTDATABASE”].ConnectionString;

Do not forget to add exceptions handling 🙂

Notes:
1. ConfigurationManager returns all settings as a strings, so you are able to use .Parse methods to get integer, bool, decimal etc from settings string value, for example:

string sPort = ConfigurationManager.AppSettings[“send_port”];
int iPort = int.Parse(sPort);

or directly:
int iPort = int.Parse(ConfigurationManager.AppSettings[“send_port”]);

2. You can add as many settings into the appSettings section as you need.

3. You can use configuration file for configuring libraries! 🙂

Happy configuring (:

Best regards,
Yahor