Skip to content

Commit 3ac5298

Browse files
author
marc
committed
Updated to support DOTNET_ENVIRONMENT
Updated to support DOTNET_ENVIRONMENT updated support of reading settings from Stackify.json for LoadSettings.
1 parent 0552e77 commit 3ac5298

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

Src/StackifyLib/Config.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ internal static string Get(string key, string defaultValue = null)
176176
{
177177
var appSettings = _configuration.GetSection("Stackify");
178178
v = appSettings[key.Replace("Stackify.", string.Empty)];
179+
180+
//Get settings from Stackify.json
181+
if (string.IsNullOrEmpty(v))
182+
{
183+
var key2 = key.Replace("Stackify.", string.Empty);
184+
var stackifyJson = _configuration.GetSection(key2);
185+
v = stackifyJson.Value;
186+
}
179187
}
180188
#endif
181189

@@ -291,17 +299,26 @@ private static string TryGetValue(JToken jToken, string key)
291299

292300
return r;
293301
}
294-
public static string GetEnvironment(JObject envName)
302+
public static string GetEnvironment(JObject envName = null)
295303
{
296-
var environmentName = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
297-
if (!String.IsNullOrEmpty(environmentName))
304+
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
305+
var DotnetEnvironment = System.Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
306+
307+
if (!String.IsNullOrEmpty(ASPEnvironment))
298308
{
299-
return environmentName;
309+
return ASPEnvironment;
300310
}
311+
else if (!String.IsNullOrEmpty(DotnetEnvironment))
312+
{
313+
return DotnetEnvironment;
314+
}
315+
else
316+
{
317+
var environmentName = TryGetValue(envName, "Environment") ?? Environment;
301318

302-
environmentName = TryGetValue(envName, "Environment") ?? Environment;
303-
304-
return environmentName;
319+
return environmentName;
320+
}
321+
305322
}
306323
}
307324
}

samples/CoreConsoleApp/Program.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ namespace CoreConsoleApp
1919
{
2020
public class Program
2121
{
22-
private static readonly JsonLoadSettings Settings = new JsonLoadSettings { CommentHandling = CommentHandling.Ignore, LineInfoHandling = LineInfoHandling.Ignore };
2322
static void Main(string[] args)
2423
{
2524
var builder = new ConfigurationBuilder()
2625
.SetBasePath(Directory.GetCurrentDirectory())
27-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
26+
.AddJsonFile("Stackify.json", optional: true, reloadOnChange: true);
2827

2928
var config = builder.Build();
3029
config.ConfigureStackifyLogging();
@@ -35,10 +34,7 @@ static void Main(string[] args)
3534
StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage;
3635
StackifyLib.Utils.StackifyAPILogger.LogEnabled = true;
3736

38-
39-
string filePath = "C:\\Source\\stackify-api-dotnet\\samples\\CoreConsoleApp\\Stackify.json";
40-
41-
Config.ReadStackifyJSONConfig();
37+
//Config.ReadStackifyJSONConfig();
4238

4339
//NLogTest();
4440
Console.WriteLine($"Stackify Config AppName: {StackifyLib.Config.AppName}");
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"AppName": "CoreConsoleApp",
3-
"Environment": "Dev",
4-
"ApiKey": "sampleKey"
2+
"AppName": "CoreConsoleApp_stackifyjson",
3+
"Environment": "Dev_stackifyjson",
4+
"ApiKey": "sampleKey_stackifyjson"
55
}

samples/CoreWebApp/Stackify.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"AppName": "CoreWebApp",
3-
"Environment": "Dev",
4-
"ApiKey": "sampleKey"
2+
"AppName": "CoreWebApp_stackifyjson",
3+
"Environment": "Dev_stackifyjson",
4+
"ApiKey": "sampleKey_stackifyjson"
55
}

samples/CoreWebApp/Startup.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ public Startup(IHostingEnvironment env)
2929
StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage;
3030
StackifyLib.Utils.StackifyAPILogger.LogEnabled = true;
3131

32-
/*var builder = new ConfigurationBuilder()
32+
var builder = new ConfigurationBuilder()
3333
.SetBasePath(env.ContentRootPath)
34-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
35-
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
34+
.AddJsonFile("Stackify.json", optional: true, reloadOnChange: true)
35+
.AddJsonFile($"Stackify.{env.EnvironmentName}.json", optional: true)
3636
.AddEnvironmentVariables();
3737
Configuration = builder.Build();
3838
Configuration.ConfigureStackifyLogging();
3939

40-
StackifyLib.Config.Environment = env.EnvironmentName;*/
40+
//StackifyLib.Config.Environment = env.EnvironmentName;
4141

42-
string filePath = "C:\\Source\\stackify-api-dotnet\\samples\\CoreWebApp\\Stackify.json";
43-
44-
Config.ReadStackifyJSONConfig();
42+
//Config.ReadStackifyJSONConfig();
4543
Debug.WriteLine(StackifyLib.Config.AppName);
4644
Debug.WriteLine(StackifyLib.Config.Environment);
4745
Debug.WriteLine(StackifyLib.Config.ApiKey);

0 commit comments

Comments
 (0)