ParkSquare.ConfigDump
8.0.3
dotnet add package ParkSquare.ConfigDump --version 8.0.3
NuGet\Install-Package ParkSquare.ConfigDump -Version 8.0.3
<PackageReference Include="ParkSquare.ConfigDump" Version="8.0.3" />
paket add ParkSquare.ConfigDump --version 8.0.3
#r "nuget: ParkSquare.ConfigDump, 8.0.3"
// Install ParkSquare.ConfigDump as a Cake Addin #addin nuget:?package=ParkSquare.ConfigDump&version=8.0.3 // Install ParkSquare.ConfigDump as a Cake Tool #tool nuget:?package=ParkSquare.ConfigDump&version=8.0.3
.Net Core Configuration Debug Dump
Background
Configuration in modern .Net frameworks 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:
- JSON file (appsettings.json)
- Environment variables
- Command line arguments
- 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 this library comes in!
Once added, on startup (or at a time of your choosing) you will see all key/value pairs listed, similar to the example log output below, depending on which ILogger
you have configured.
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
.
15:03:18 DBG applicationName=ParkSquare.Demo [ChainedConfigurationProvider]
15:03:18 DBG AUTO_RELOAD_WS_ENDPOINT=ws://localhost:58484/ParkSquare.Demo/ [ChainedConfigurationProvider]
15:03:18 DBG HTTPS_PORT=5001 [ChainedConfigurationProvider]
15:03:18 DBG AllowedHosts=* [JsonConfigurationProvider/appsettings.json]
15:03:18 DBG Serilog:Enrich:0=FromLogContext [JsonConfigurationProvider/appsettings.json]
15:03:18 DBG Serilog:Enrich:1=WithMachineName [JsonConfigurationProvider/appsettings.json]
15:03:18 DBG Serilog:Enrich:2=WithThreadId [JsonConfigurationProvider/appsettings.json]
15:03:18 DBG Smtp:Port=25 [JsonConfigurationProvider/appsettings.json]
15:03:18 DBG Smtp:ServerName=localhost [JsonConfigurationProvider/appsettings.json]
15:03:18 DBG :ASPNETCORE_BROWSER_TOOLS=true [EnvironmentVariablesConfigurationProvider]
15:03:18 DBG ALLUSERSPROFILE=C:\ProgramData [EnvironmentVariablesConfigurationProvider]
15:03:18 DBG APPDATA=C:\Users\Jon\AppData\Roaming [EnvironmentVariablesConfigurationProvider]
Getting Started
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.
Console Apps
Install the ParkSquare.ConfigDump
package using the command above, or using Visual Studio's package manager. Now add a new JSON file to your project, and call it appsettings.json
.
Build you configuration object:
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
Now register the required dependencies. This includes adding a logging provider, the config itself, and the config dump implementation:
var services = new ServiceCollection()
.AddLogging(builder => builder.AddConsole())
.AddSingleton<IConfiguration>(config)
.AddConfigDump() // Enables config dump
.BuildServiceProvider()
Log configured values at startup
At this point, you can either call .DumpConfig()
on your service collection to log out the config key/value pairs:
// 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();
}
}
Log configured values inside your own code
Alternatively, you can inject IConfigurationDumper
into your own classes and call Dump()
whenever suits you:
// 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();
}
}
Asp.Net API & Web Apps
To use with Asp.Net Core, add the ParkSquare.ConfigDump.AspNetCore
package.
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.
Azure Function Apps
To use with Azure Function apps, add the ParkSquare.ConfigDump
package and then add services.AddConfigDump()
followed by services.DumpConfig()
.
var host = new HostBuilder()
.ConfigureServices((context, services) =>
{
services.AddConfigDump();
}
It is also possible to inject IConfigurationDumper
into your own classes and call Dump()
whenever suits you, as per the other usage scenarios.
Changing Log Level
By default, your config will be logged using a log level of 'information'. If you want to change this to some other log level, such as 'debug', simply pass the required level to the .AddConfigDump()
call:
services.AddConfigDump(LogLevel.Debug);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.FileExtensions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
-
net6.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.FileExtensions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.FileExtensions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ParkSquare.ConfigDump:
Package | Downloads |
---|---|
ParkSquare.ConfigDump.AspNetCore
Use this version for Asp.Net projects. Dump out all your .Net Core and ASP.Net Core config on startup, together with the configuration provider source that value came from. Ideal for troubleshooting when overriding values via environment variables in containerized environments such as Azure, Amazon AWS and Rancher. Enabled with simple services.AddConfigDump() call at the end of your ConfigureServices() method. Uses any configured ILogger. |
GitHub repositories
This package is not used by any popular GitHub repositories.