image QAToolKit part 2 – Swagger library image Run Docker for Windows in “mixed” mode by running Windows and Linux containers together

.NET IConfiguration pipeline viewer

When developing .NET Web APIs, we almost always need to load some configuration to function properly. This can be database connection strings, email configuration, Azure blob storage configuration, etc.

That configuration can come from JSON files, Environment variables, secrets.json file, Azure Keyvault, and many other providers. We can also write our own configuration provider that will load configuration from a custom source.

If you load configuration from many sources and overriding them through the configuration pipeline, you found it hard to debug. You sometimes do not know where the configuration came from or what the value that the Web API will use is. That’s why I’ve written this simple configuration extension function in C# that prints the final configuration that came out of the configuration pipeline.

You normally have something like this to build a configuration from different sources:

There are two classes below: ConfigurationExtensions and ConfigurationInfo.

As you can see, we take IConfiguration and service name as an input parameter in the ProcessConfigurationProviders and then construct a flat list of configurations from all the providers. We can extend this by filtering out only specific configurations, printing only in specific environments, or mask the output values (remember configuration can also contain secrets, and you do not want those in logs).

Example of calling the code above.

And this is the output. You get a table of configurations, their flat names, from which provider it came from, and the masked value, so we do not expose them in the logs files. This was run on my development machine. Now imagine that you are running this application in the Azure cloud and you use Azure KeyVault. You will immediately see where the specific secret is coming from in case you need to debug the application.

I hope your configuration debugging got just a bit easier :). I might also pack this into a NuGet package and extend it a bit. There is a lot of room for improvement.

Ta-Da!

About Miha
I've always been fascinated by computers. I am happy that I can work in this creative field of software development, where I can solve problems. I am also an avid cyclist :) and I love to learn...a lot!
Related Posts
  • All
  • By Author
  • By Category
  • By Tag

Leave a Reply