AL NET6
AL NET6

Reputation: 1

I'm implementing similar wrapper like this. But, my question is how do I add configuration in appsettings.json to this

Refer to this thread Implementation and usage of logger wrapper for Serilog

In the Program.cs file in .NET 6

IConfiguration conf = (new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json") .Build());

var builder = WebApplication.CreateBuilder(options); builder.Services.AddScoped();

Upvotes: 0

Views: 67

Answers (1)

Kofi Sammie
Kofi Sammie

Reputation: 3647

To add configuration to your .NET Core application, you can use the ConfigurationBuilder class and read the values from the appsettings.json file.

Here's an example of how to do this:

Create a new instance of ConfigurationBuilder:

var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Build the configuration object:

IConfigurationRoot configuration = builder.Build();

Use the GetValue method to read the configuration value:

var value = configuration.GetValue<string>("YourConfigKey");

So, for example, if you have a setting called WrapperApiKey, your appsettings.json file could look like this:

{
  "WrapperApiKey": "your_api_key_here"
}
And you could read the value in your code like this:

var apiKey = configuration.GetValue<string>("WrapperApiKey");

Make sure to replace "YourConfigKey" with the actual key you want to read from the appsettings.json file.

Upvotes: 0

Related Questions