Skip to content

Commit f01641f

Browse files
author
marc
committed
check environment variables before reading stackify.json
Update logic on overriding environment from ASPNETCORE_ENVIRONMENT and DOTNET_ENVIRONMENT environment variables when reading which Stackify.json to be used.
1 parent 63e92f0 commit f01641f

File tree

6 files changed

+53
-29
lines changed

6 files changed

+53
-29
lines changed

Src/StackifyLib/Config.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,26 @@ public static void ReadStackifyJSONConfig()
221221
{
222222
try
223223
{
224+
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
225+
var DotnetEnvironment = System.Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
224226
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
225-
string jsonPath = Path.Combine(baseDirectory, "Stackify.json");
226-
string json;
227+
string jsonPath = string.Empty;
228+
string json = string.Empty;
229+
230+
ASPEnvironment = "Production";
231+
232+
if (!String.IsNullOrEmpty(ASPEnvironment))
233+
{
234+
jsonPath = Path.Combine(baseDirectory, $"Stackify.{ASPEnvironment}.json");
235+
}
236+
else if (!String.IsNullOrEmpty(DotnetEnvironment))
237+
{
238+
jsonPath = Path.Combine(baseDirectory, $"Stackify.{DotnetEnvironment}.json");
239+
}
240+
else
241+
{
242+
jsonPath = Path.Combine(baseDirectory, "Stackify.json");
243+
}
227244

228245
if (File.Exists(jsonPath))
229246
{
@@ -241,7 +258,20 @@ public static void ReadStackifyJSONConfig()
241258
else
242259
{
243260
string iisBaseDirectory = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
244-
string iisJsonPath = Path.Combine(iisBaseDirectory, "Stackify.json");
261+
string iisJsonPath = string.Empty;
262+
263+
if (!String.IsNullOrEmpty(ASPEnvironment))
264+
{
265+
iisJsonPath = Path.Combine(iisBaseDirectory, $"Stackify.{ASPEnvironment}.json");
266+
}
267+
else if (!String.IsNullOrEmpty(DotnetEnvironment))
268+
{
269+
iisJsonPath = Path.Combine(iisBaseDirectory, $"Stackify.{DotnetEnvironment}.json");
270+
}
271+
else
272+
{
273+
iisJsonPath = Path.Combine(iisBaseDirectory, "Stackify.json");
274+
}
245275

246276
if (File.Exists(iisJsonPath))
247277
{
@@ -295,7 +325,7 @@ public static void ReadStackifyJSONConfig(string filePath)
295325
public static void SetStackifyObj(JObject obj)
296326
{
297327
AppName = TryGetValue(obj, "AppName") ?? AppName;
298-
Environment = GetEnvironment(obj);
328+
Environment = TryGetValue(obj, "Environment") ?? Environment;
299329
ApiKey = TryGetValue(obj, "ApiKey") ?? ApiKey;
300330
}
301331

@@ -327,26 +357,5 @@ private static string TryGetValue(JToken jToken, string key)
327357

328358
return r;
329359
}
330-
public static string GetEnvironment(JObject envName = null)
331-
{
332-
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
333-
var DotnetEnvironment = System.Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
334-
335-
if (!String.IsNullOrEmpty(ASPEnvironment))
336-
{
337-
return ASPEnvironment;
338-
}
339-
else if (!String.IsNullOrEmpty(DotnetEnvironment))
340-
{
341-
return DotnetEnvironment;
342-
}
343-
else
344-
{
345-
var environmentName = TryGetValue(envName, "Environment") ?? Environment;
346-
347-
return environmentName;
348-
}
349-
350-
}
351360
}
352361
}

samples/CoreConsoleApp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class Program
2222
static void Main(string[] args)
2323
{
2424
var builder = new ConfigurationBuilder()
25-
.SetBasePath(Directory.GetCurrentDirectory())
26-
.AddJsonFile("Stackify.json", optional: true, reloadOnChange: true);
25+
.SetBasePath(Directory.GetCurrentDirectory());
26+
//.AddJsonFile("Stackify.json", optional: true, reloadOnChange: true);
2727

2828
var config = builder.Build();
2929
config.ConfigureStackifyLogging();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"AppName": "CoreConsoleApp_stackifyjson_Prod",
3+
"Environment": "Prod_stackifyjson",
4+
"ApiKey": "sampleKey_stackifyjson_Prod"
5+
}

samples/CoreWebApp/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
"launchBrowser": true,
1414
"launchUrl": "api/values",
1515
"environmentVariables": {
16-
"ASPNETCORE_ENVIRONMENT": "Development"
16+
"DOTNET_ENVIRONMENT": "Production"
1717
}
1818
},
1919
"CoreWebApp": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
2222
"launchUrl": "http://localhost:5000/api/values",
2323
"environmentVariables": {
24-
"ASPNETCORE_ENVIRONMENT": "Development"
24+
"DOTNET_ENVIRONMENT": "Production"
2525
}
2626
}
2727
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"AppName": "CoreWebApp_stackifyjson_dev",
3+
"Environment": "Development",
4+
"ApiKey": ""
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"AppName": "CoreWebApp_stackifyjson_prod",
3+
"Environment": "Production",
4+
"ApiKey": ""
5+
}

0 commit comments

Comments
 (0)