How do I configure base class libraries in my app.config file? - c#

I've found a couple of snippets of information pertaining to app.config/web.config that hints at almost codeless configuration of BCL components directly through the app.config. However, given the amount of tags suggested by the intellisense within the app.config, it suggests that there is a huge amount of possibilities for this that I can't find any useful information for.
Is there any documentation that supports this particular area of configuration files? I can find plenty of information on storing/retrieving configuration information and a small amount regarding writing custom configuration sections which I'm familiar with, but I cannot find any information regarding configuring BCL components this way. Does anyone have any reference material for this?
One example I've come across is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="Console"
type="System.Diagnostics.ConsoleTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
traceOutputOptions="Timestamp" />
</listeners>
</trace>
<switches>
<add name="Logging.Program.Listener" value="Error" />
</switches>
</system.diagnostics>
</configuration>
Which may be consumed using code in a similar fashion to this:
class Program
{
private static TextWriterTraceListener tw = new TextWriterTraceListener();
private static TraceSwitch ts = new TraceSwitch("Logging.Program.Listener", "Default Logging Level", "Off");
static void Main(string[] args)
{
Trace.Listeners.Add(tw);
try
{
throw (new EntryPointNotFoundException());
}
catch (EntryPointNotFoundException ex)
{
string TraceMessage = "Trace {0}: {1}";
Trace.WriteLineIf(ts.TraceError, String.Format(TraceMessage, TraceLevel.Error, "Error Level Message"));
Trace.WriteLineIf(ts.TraceWarning, String.Format(TraceMessage, TraceLevel.Warning, "Warning Level Message"));
Trace.WriteLineIf(ts.TraceInfo, String.Format(TraceMessage, TraceLevel.Info, "Info Level Message"));
Trace.WriteLineIf(ts.TraceVerbose, String.Format(TraceMessage, TraceLevel.Verbose, "Verbose Level Message"));
}
}
}

One useful resource is the machine-level configuration files. The actual files are bare-bones, but there are ".comments" files alongside them that give fairly detailed examples of what can be achieved. For example, take a look in
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config.comments
That will give you some idea of what's achievable. Anywhere where you see collection elements, as in the case of the <traceSwitches> and <traceListeners> elements, the individual <add> elements contained within may vary depending on what you are adding (i.e. the specific attributes on those <add> elements will vary depending on exactly what you're adding to the collection). For this, you'll need to consult specific areas of documentation, but searching for the <traceSwitches> element in MSDN ought to serve as a decent starting point there.

They're all configurable this way. That's why you're not finding anything.
Ok, maybe not all, but certainly most. If you want to know, Use Reflector to find all the derived classes of System.configuration.ConfigurationSection, etc.

Related

Custom config elements collection in Web.config - Using a key to choose

Intro
I'm developing a WebApp built on C# ASP.NET.
I've been researching creating a "Custom Configuration" section with child elements in the Web.config file, and I've hit a bit of a snag when it comes to consuming the keys/values in the data.
I seem to be going round in circles and I don't know how to tackle the issue I'm having.
Situation
I have a few different Connection Strings defined in the Web.Config file, in the <connectionStrings> section. They are for dev, test, and live databases.
<connectionStrings>
<add name="connectionOne" connectionString="..." providerName="..." />
<add name="connectionTwo" connectionString="..." providerName="..." />
<add name="connectionThree" connectionString="..." providerName="..." />
</connectionStrings>
The WebApp is currently hard-coded to use one of these connection strings - if I need to change which one to use, I need to re-compile.
Desired Functionality
I'd like to define a section in the Web.config, let's say DbSettings.
In that, I'd then like to be able to define some child elements for, let's say DbSettings, in which I could define dbConnectionName, foo, bar etc. as attributes.
For example:
<dbSettings>
<dbSetting key="DbSetting1"
dbConnectionName="connectionOne"
foo="fooOne"
bar="barOne" />
... and so on
</dbSettings>
Then, perhaps in the <appSettings> section, define which of these DbSettings elements I want to use to get the settings from:
<appSettings>
<add name="dbSettingsKey" value="DbSetting1" />
</appSettings>
Desired Web.config section
Here is a fuller example of what I'd imagine my Web.config file to look like:
Connection Strings
<connectionStrings>
<add name="connectionOne" connectionString="..." providerName="..." />
<add name="connectionTwo" connectionString="..." providerName="..." />
<add name="connectionThree" connectionString="..." providerName="..." />
</connectionStrings>
App Settings
<add key="dbSettingsKey" value="DbSetting1" /> // or 2, or 3 etc.
DbSettings (custom section)
<dbSettings>
<dbSetting key="DbSetting1"
dbConnectionName="connectionOne"
foo="fooOne"
bar="barOne" />
<dbSetting key="DbSetting2"
dbConnectionName="connectionTwo"
foo="fooTwo"
bar="barTwo" />
<dbSetting key="DbSetting3"
dbConnectionName="connectionThree"
foo="fooThree"
bar="barThree" />
</dbSettings>
My question...
How the devil am I going to get this desired functionality in the C# code?
I've read loads on "creating your own custom section", and similarly "creating a custom config collection". But, I just can't seem to glue it all together to apply for my situation.
I'd like to be able to have a class (like the one I'm using at the moment with the hard-coded strings), which I can reference necessary properties (as I am doing, at the moment) - and then the code can dynamically load the correct settings at run-time from the sections I've described above.
As always, thank you in advance for your suggestions and help.
I agree with the comments. The way this is usually done is you deploy a different web.config to each environment. When your deployment group (or you) deploys, you deploy everything EXCEPT the web.config unless you have changes to push.
In answer to your other question, adding a custom section is not trivial. It's quite a bit of work. Custom section handler which requires a whole bunch of configuration element classes and a bunch of configuration element collection classes... and then, if you want it to "work" correctly, you also need to create a schema and register that with the IDE, etc.
For your particular case, I'd just do it the "normal" way :).

Variables in XML configuration

I'm trying to use xml configuration file in my project. Now it looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="replication" type="Project.Replication.ReplicationConfigSection, Project.Replication" />
<section name="processing" type="Project.Processing.ProcessingConfigSection, Project.Processing" />
</configSections>
<replication>
<streams>
<stream name="STREAM_DATA_14360" />
</streams>
</replication>
<processing dataStream="STREAM_DATA_14360" />
</configuration>
It works OK, but I'm confused with duplicates in it ("STREAM_DATA_14360").
Can you remind me, how to create variables in XML or something for data reusing to be acceptable in application configuration?
UPDATE:
In real life my configuration has much more sections. There is a value, which apeears in many of this sections: STREAM_DATA_14360. So I want to be able to change this value only in one place of config file, and in other places to use reference to it.
Speed of changing configuration - is the first reason for it.
Size of a file is a second, because values can be huge: STREAM_INFO_FUTURE_SESSION_CONTENTS_12421 (that is third-party names)
You can simply add this value in <appSettings> and access it as you are saying.
You can do this as below:
<appSettings>
<add key="StreamName" value="STREAM_DATA_14360"/>
</appSettings>
In the code, you can access it as below:
string streamName = ConfigurationManager.AppSettings["StreamName"];
Make sure to add reference to System.Configuration assembly before using this.
XML doesn't have any native expansion macros or templating - any scenario would require that you do a preprocess step or have the code that reads the config involved in substituting the value.
If those aren't redacted names though, it seems a simple search/replace would solve the problem without much of a concern on false positives.
You could put something together with T4 templates as a preprocessor, whether that's worth it really depends on how often you expect to modify this file.
It should also be possible to shoehorn the web.config transformation engine into doing the replacements, but you may have to write some hosting code for the XDT engine depending on how your config file is setup.
Apart from using external code that might (or might not) facilitate your life, you can define your own classes that inherit from ConfigurationSection, wherein you define and encapsulate your key/value pairs and use the ConfigurationProperty attribute.
Have look at http://msdn.microsoft.com/en-us/library/2tw134k3.aspx for more info on How to: Create Custom Configuration Sections Using ConfigurationSection.
EDIT: you can make references in xsd (check here)
Thanks for your answers. I agree with Mark, there's no support of variables or references in XML. But, in my case there's much simpler solution. I feel stupid now, but hope that it will help another slowpoke too.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="global" type="Project.GlobalConfigSection, Project" />
<section name="replication" type="Project.Replication.ReplicationConfigSection, Project.Replication" />
<section name="processing" type="Project.Processing.ProcessingConfigSection, Project.Processing" />
</configSections>
<global>
<streamNames>
<streamName name="STREAM_DATA_14360" id="1"/>
</streamNames>
</global>
<replication>
<streams>
<stream nameId="1" />
</streams>
</replication>
<processing dataStreamId="1" />
</configuration>
Consequence: need to edit code to use global section as a source of all long names
Advantage: fast renaming, reusability of values

System.Diagnostics Traces wildcard for source name

I have my Logging set up to use a different TraceSource for each Class.
Is it possible to Configure a wildcard that writes events for all Sources?
<system.diagnostics>
<sources>
<source name ="wildcard" switchValue="Warning">
<listeners>
<add name="textlog" />
</listeners>
</source>
<source name="MySpecificClass" switchValue="All">
<listeners>
<add name="textlog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="textlog"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="Log.log">
</add>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
I'm not aware of a built in way to do that automatically. However, if you have a look at the TraceLogger in Castle's git repository, you can see that they have essentially wrapped and extended TraceSource to support "hierarichal" naming.
https://github.com/castleproject/Core/blob/master/src/Castle.Core/Core/Logging/TraceLogger.cs
I would copy the code here, but it might not be proper to just cut and paste there code into SO.
I can explain how the ideas presented in the class could work for your (without you having to use Castle)
In essence, in your client code (that wants to log stuff), you would create an instance of your "logger" (rather than TraceSource). As input to the logger, you would give, for example, the fully qualified class name. Inside the constructor, use the input name to try to resolve a TraceSource. If there is a TraceSource configured with that name, use that TraceSource to do the work. If not, trim off the rightmost part of the fully qualified name. Try to resolve a TraceSource with that name. If there is a TraceSource configured with that name, use it. And so on. If you don't find any TraceSources, then don't log anything from your "logger". You could add on the ability to recognize a TraceSource that has been configured with a wildcard name (""). If you never find a TraceSource using the name trimming technique and if there is a "" TraceSource, use the "*" TraceSource as the fallback.
So, you might have something like this:
class MyTraceSource
{
private TraceSource ts;
public MyTraceSource(string name)
{
ResolveTraceSource(name);
}
private void ResolveTraceSource(string name)
{
//Check for a configured TraceSource from most qualified name (as input) to least qualified ("").
//Assume name like this: Namespace1:Namespace2:Class
//Try to resolve:
// TraceSource("Namespace1.Namespace2.Class");
// TraceSource("Namespace1.Namespace2");
// TraceSource("Namespace1");
//If you still haven't found one, try to resolve
// TraceSource("*");
}
//Implement either TraceSource API, or whatever API you prefer for logging.
}
I have actually done something like this myself as part of a prototype (that we ended up not using) and it worked pretty well for mimicking the way that you can specify loggers in log4net and NLog.
Good luck!

Add custom configuration Element at runtime

Is it possible to add an custom configuration element at runtime.
Here is my app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="NodeList"
type="Configuration.NodeListSection, NodeListConfiguration"
requirePermission="false" />
</configSections>
<NodeList>
<nodes>
<add name="Dev1" isdefault="false" description ="Dev server" />
<add name="QA1" isdefault="true" description="QA server"/>
<add name="Prod1" isdefault="false" description="Production" />
</nodes>
</NodeList>
</configuration>
Can we add more nodes at runtime using C# code.
This doesn't appear to be from a built-in configuration section. You will find that "NodesList" is an section/element that is custom written. To determine where in your codebase it is coming from look for "NodesList" at the top of your config file in the configSections element. That will point you at the class to look into.
After that, you need the class to support write operations properly.
To learn a lot more about customising configuration files there is a great series at CodeProject on the topic. In particular, the section on Saving Configuration Changes should be helpful to you.
Edit (after more info added to question):
Try something like (of course it all depends on what's in NodeListSection codebase):
using Configuration;
var nodeListSection = ConfigurationManager.GetSection("NodeList") as Configuration.NodeListSection;
var newNode = new NodeElement() { Name = "xyz", IsDefault = false, Description = "New Guy" };
nodeListSection.Nodes.Add(newNode);
Configuration.Save(ConfigurationSaveMode.Modified);
The file you have posted does not look like a normal .NET config file, but a custom XML file.
In either case - .config files are just XML files - you can open, manipulate and save them using any of the XML libraries within the BCL, such as XDocument.
However, if you want to make changes to configuration during runtime, you will need to decide whether the application should apply these changes at runtime as well and code for this, as normally a configuration file will only be read at startup.
private void AddNewKey_Config(string key, string value, string fileName)
{
var configFile = ConfigurationManager.OpenExeConfiguration(fileName);
configFile.AppSettings.Settings.Add(key, value);
configFile.Save();
}

Override config settings

I have a config file that is used in several projects, general.config, looks like:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="mykey1" value="myvalue1"/>
<add key="mykey2" value="myvalue2"/>
</appSettings>
In one of the projects, I need to override one of the two settings. So the app.config of this project looks like:
<?xml version="1.0"?>
<configuration>
<appSettings file="general.config">
<remove key="mykey1"/>
<add key="mykey1" value="anothervalue"/>
<add key="mykey3" value="myvalue3"/>
</appSettings>
</configuration>
But remove is not working here. How can I override mykey1 without breaking mykey2? add works in this case. I can get myvalue3 from ConfigurationManager.
EDIT: general.config is copied to output folder automatically when compiling. Don't worry about the path issue. Currently I got:
ConfigurationManager.AppSettings["mykey1"]
//I got "myvalue1", but I want "anothervalue" here
//that is, this item is "overrided", just like virtual methods in C#
ConfigurationManager.AppSettings["mykey2"]
//this setting will not be modified, currently it works fine
ConfigurationManager.AppSettings["mykey3"] //good
A friend of mine answered this question. From MSDN:
You can use the file attribute to
specify a configuration file that
provides additional settings or
overrides the settings that are
specified in the appSettings element.
You can use the file attribute in
source control team development
scenarios, such as when a user wants
to override the project settings that
are specified in an application
configuration file. Configuration
files that are specified in a file
attribute must have the appSettings
element rather than configuration
element as the root node.
So in this question, the settings in general.config overrides items in app.config, different from that I think(want) app.config items overrides items in general.config. Now I think I have to resolve this issue in C# code(it will inevitable looks ugly).
Your use of the file attribute to load common settings with an expectation that keys added directly to the <appSettings> element would override those common settings is understandable, but unfortunately that is not how it works.
Microsoft's intention was for the file attribute to load common settings that override the individual application's settings.
This is discussed in some detail in the Microsoft Documentation
To overcome this problem, we very occasionally declare base settings in the common file, and then appropriately named overrides in the application configuration. However this does require additional code which is a bit ugly. e.g.
var config = ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER_OVERRIDE"]
?? ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER"]
?? "ActiveMQ";
<appSettings file="common.config">
<!-- Override the common values -->
<add key="MSG_QUEUE_PROVIDER_OVERRIDE" value="RabbitMQ"/>
</appSettings>
The elements are changed from the child and what i mean by that is currently your app.config is the parent file and the values are replaced by the ones existing in General.config
Since you are using remove in the parent file what its effectively doing is removing the element you specify in app.config but after that the elements from general.config are pushed in. Now say here in General.config you say remove mykey3 which is on your app.config you will see that the final collection has no key as mykey3.
In short this is not going to work. Hope this helped you.
You can add another config file say Test.config.
<appSettings>
<add key="mykey1" value="New value"/>
</appSettings>
and in the app.config appsettings section will look like this
<appSettings file="Test.config">
<add key="mykey1" value="myvalue1"/>
</appSettings>

Categories