BenAlabaster
BenAlabaster

Reputation: 39846

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

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"));
    }
  }
}

Upvotes: 0

Views: 679

Answers (2)

Dave Cluderay
Dave Cluderay

Reputation: 7426

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.

Upvotes: 2

John Saunders
John Saunders

Reputation: 161791

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.

Upvotes: 0

Related Questions