.Net Core Configuration Debug Dump

ParkSquare.ConfigDump

BUILD STATUS Continuous Integration Build Status

INSTALLATION

Install-Package ParkSquare.ConfigDump

DESCRIPTION

This package will conveniently list out all configuration settings on startup, together with the Configuration Provider they came from. Works with .Net Core and Asp.Net Core.

Documentation

Configuration in .Net Core is based on key-value pairs, and configuration providers. These providers read data from various places, combine them all together and then present them to your application.

Here are some of the more popular configuration providers:

  • appsettings.json
  • Command line arguments
  • Environment variables
  • In-memory Dictionary

If a key/value pair appears in more than one provider, the one registered last will take precedence. Usually, your app will have values set in appsettings.json. When deploying to your test and live environments, some of these may be transformed - for example by Octopus Deploy or some other similar mechanism. Furthermore, especically when deploying to Microsoft Azure or using Docker containers, you may additionally override values using environment variables.

It can be troublesome to work out if the final view of your config being presented to the app is as you expect. That's where our ConfigDump Nuget package comes in. Once added to your project, a simple line added to Startup.cs will cause a list of all config key/value pairs to be dumped out, complete with the config provider they came from. It will use whichever ILogger implementation(s) you are using, so works great with things such as Serilog, console or file logging.

Once added, on startup (or at a time of your choosing) you should see all key/value pairs listed, similar to the screen shot below, depending on which ILogger you have configured. The example highlights the key in green, the value in red and the configuration source in yellow. If the configuration provider has a file source, as the JsonConfigurationProvider does, then the filename will also be shown, e.g. appsettings.json and appsettings.development.json.

Config dump example

How To Use with .Net Core Console Apps

Install the ParkSquare.ConfigDump package using the command above, or using Visual Studio's package manager. Add a new JSON file to your project, and call it appsettings.json. Don't forget to set the config file so it gets copied into the bin directory on build. You will then be able to build your configuration object:


    var config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", true, true)
        .Build();

Now you need to register your dependencies. This includes adding a logging provider, the config itself, and the config dump implementation. The .AddConfigDump() extension method can be used for the latter:


    var services = new ServiceCollection()
        .AddLogging(builder => builder.AddConsole())
        .AddSingleton<IConfiguration>(config)
        .AddConfigDump()
        .BuildServiceProvider()

At this point, you can either call .DumpConfig() on your service collection to log out the config key/value pairs, or you can inject IConfigurationDumper into your own classes and call Dump() whenever suits you.


    // Complete example: log config values on startup

    static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", true, true)
            .Build();

        using (var services = new ServiceCollection()
            .AddLogging(builder => builder.AddConsole())
            .AddSingleton<IConfiguration>(config)
            .AddConfigDump()
            .BuildServiceProvider())
            {
                services.DumpConfig();
            }
    }


    // Example of injecting into your own class
    // Call .AddConfigDump() to register dependencies, or register them manually

    public class DemoClass
    {
        private readonly IConfigurationDumper _configurationDumper;

        public DemoClass(IConfigurationDumper configurationDumper)
        {
            _configurationDumper = configurationDumper;
        }

        public void DoSomething()
        {
            // ....other code....

            _configurationDumper.Dump();
        }
    }

How To Use with Asp.Net Core API and Web Apps

To use with Asp.Net Core, add the ParkSquare.ConfigDump.AspNetCore package:


    Install-Package ParkSquare.ConfigDump.AspNetCore

Open Startup.cs and add the following to the ConfigureServices() method. Make sure you use the extension method from ParkSquare.ConfigDump.AspNetCore and not the one in ParkSquare.ConfigDump, as it sets up additional dependencies specific to Asp.Net Core. The call to AddConfigDump() should be just before AddMvc().


    using ParkSquare.ConfigDump.AspNetCore;
    
    // ....other code....

    public void ConfigureServices(IServiceCollection services)
    {
        // ....other registrations....

        // Add this just before AddMvc()
        services.AddConfigDump();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

When your app starts up, your config will be logged out before it starts accepting requests.