Skip to content
Michael Brown edited this page Apr 8, 2020 · 4 revisions

Welcome to the AnyConfig wiki!

AnyConfig

A .net configuration library to make configuration of multi-target applications easier.

Description

AnyConfig makes configuration on solutions which mix .Net Core and .Net Framework projects easier. It abstracts away ConfigurationManager and IConfiguration loading with no dependencies on Microsoft implementation. You can instead use ConfigurationManager to load either json or xml configuration files, as well as the IConfiguration interface. This allows you to upgrade to json configuration files even for older projects without changing any code!

Usage

Simplest usage is using the dedicated generics interface:

var isEnabled = Config.Get<bool>("IsEnabled");

You can specify a default value for settings that aren't required to exist:

var intValue = Config.Get<int>("Port", 443);

You can also bind your own configuration class:

var testConfiguration = Config.Get<MyTestConfiguration>();

Grab an IConfiguration for .net core without any Microsoft extensions:

var config = Config.GetConfiguration();
var testConfiguration = config.Get<MyTestConfiguration>();

If you need, use the legacy ConfigurationManager:

var isEnabled = ConfigurationManager.AppSettings["IsEnabled"];

The built-in ConfigurationManager supports generics:

var isEnabled = ConfigurationManager.AppSettings["IsEnabled"].As<bool>();

It also supports reading from json:

ConfigurationManager.ConfigurationFilename = "appsettings.json";
var isEnabled = ConfigurationManager.AppSettings["IsEnabled"].As<bool>();
// appsettings.json
{
  "IsEnabled": true
}

Advanced Usage

JSON configuration

The following samples will use the following appsettings.json JSON:

{
  "MyTestConfiguration": {
    "IsEnabled": true,
    "Name": "My name",
    "Url": "http://localhost",
    "CustomEnum": "EnumValue",
    "Server": {
      "Port": 1234,
      "IP": "127.0.0.1"
    }
  },
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Error"
    }
  }
}

Get a configuration from a specified section:

var config = Config.Get<MyTestConfiguration>("MyTestConfiguration");
var logging = Config.Get<Logging>("Logging");

Define a class for your entire log and get it from the root:

var config = Config.Get<Configuration>();

//
public class Configuration
{
    public MyTestConfiguration MyTestConfiguration { get; set; }
    public Logging Logging { get; set; }
}

XML configuration

The following samples will use the following App.config XML:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <connectionStrings>
    <add name="TestConnection" connectionString="host=localhost;"/>
  </connectionStrings>
  <appSettings>
    <add key="Name" value="TestValue" />
    <add key="Url" value="http://localhost" />
    <add key="IsEnabled" value="true" />
    <add key="Port" value="1234" />
    <add key="CustomEnum" value="EnumValue" />
    <add key="TestConfigurationObjectName" value="TestName" />
    <add key="TestConfigurationObjectValue" value="TestValue" />
  </appSettings>
  <nlog>
    <targets async="true">
      <target type="Trace" name="trace" layout="${longdate}|${level:uppercase=true}|${itn-activity-id}|${logger}|${message}|${exception:format=tostring}" />
    </targets>
    <rules>
      <logger name="*" minLevel="Trace" appendTo="trace" />
    </rules>
  </nlog>
</configuration>

Get values from AppSettings:

var isEnabled = Config.Get<bool>("IsEnabled");

Get a section:

var isEnabled = Config.Get<NLog.Config.XmlLoggingConfiguration>("IsEnabled");

You can also use the legacy ConfigurationManager:

var isEnabled = ConfigurationManager.AppSettings["IsEnabled"].As<bool>();

Get a custom section using ConfigurationManager:

var nlogConfig = ConfigurationManager.GetSection("nlog").As<NLog.Config.XmlLoggingConfiguration>();

See the ConfigurationManager for more legacy usage examples and information.