ASP.NET Core da Program cs de Main’deki ortam adına göre appsetting e erişmek ve yönetmek
Apnet.Core projede Startup.cs de ortam değişkenine göre projeye ayağa kaldırabiliyoruz. TEST,DEV,PROD ortamları için projemizi çalıştırabiliyoruz.
Fakat program.cs de ki bazı configurasyonlar için ortama göre değişen appsettings den okunan değerlere ihtiyaç var. Benim durumumda exceptionless konfigurasyonu Program.cs içinde ki main methodunda. Ama burada bir bağımlılık yönetmeye çalışmadan direk ortam değişkinine ve değerlerine ihtiyacım vardı. Zaten startup.cs de IConfiguration ve IWebHostEnvironment interfaceleri impelemente ederek bu süreci yönetebiliyoruz.
Peki program.cs de nasıl yapacağım? Tam da aşağıdaki kod parçası benim işimi gördü. Artık exceptionless i ortama göre yönetebiliyorum.
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
string exceptionLessUrl = string.Empty;
string exceptionLessApiKey = string.Empty;
switch (environment)
{
case "Production":
var configProduction = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: false).Build();
exceptionLessUrl = configProduction.GetSection("KipConfig:ExceptionLess:Url").Value;
exceptionLessApiKey = configProduction.GetSection("KipConfig:ExceptionLess:Key").Value;
break;
case "Development":
var configDevelopment = new ConfigurationBuilder().AddJsonFile("appsettings.Development.json", optional: false).Build();
exceptionLessUrl = configDevelopment.GetSection("KipConfig:ExceptionLess:Url").Value;
exceptionLessApiKey = configDevelopment.GetSection("KipConfig:ExceptionLess:Key").Value;
break;
default:
break;
}
ExceptionlessClient.Default.Configuration.ApiKey = exceptionLessApiKey;
ExceptionlessClient.Default.Configuration.ServerUrl = exceptionLessUrl;"KipConfig": {
"JsReport": {
"Port": "****"
},
"CorsOrigins": [
"https://localhost:44352",
"https://*******",
"https://********",
"https://*******"
],
"ExceptionLess": {
"Url": "http://exceptionless.*****.***.**:*****",
"Key": "*************************************"
}
},







