Skip to content

Commit 0552e77

Browse files
author
marc
committed
Update reading Stackify.json
get the base path when reading stackify.json add support for ASPNETCORE_ENVIRONMENT when setting environmentName.
1 parent eb649cc commit 0552e77

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

Src/StackifyLib/Config.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Reflection;
56
using Newtonsoft.Json.Linq;
67
using StackifyLib.Utils;
78

@@ -202,6 +203,34 @@ internal static string Get(string key, string defaultValue = null)
202203
return v;
203204
}
204205

206+
public static void ReadStackifyJSONConfig()
207+
{
208+
try
209+
{
210+
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
211+
string jsonPath = Path.Combine(baseDirectory, "Stackify.json");
212+
213+
string json;
214+
215+
using (var fs = new FileStream(jsonPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
216+
{
217+
using (var sr = new StreamReader(fs))
218+
{
219+
json = sr.ReadToEnd();
220+
}
221+
}
222+
223+
var obj = JObject.Parse(json, Settings);
224+
Config.SetStackifyObj(obj);
225+
}
226+
catch (Exception ex)
227+
{
228+
StackifyAPILogger.Log("#Config #ReadStackifyJSONConfig failed", ex);
229+
}
230+
231+
}
232+
233+
#if JSONTEST
205234
public static void ReadStackifyJSONConfig(string filePath)
206235
{
207236
try
@@ -225,11 +254,12 @@ public static void ReadStackifyJSONConfig(string filePath)
225254
}
226255

227256
}
257+
#endif
228258

229259
public static void SetStackifyObj(JObject obj)
230260
{
231261
AppName = TryGetValue(obj, "AppName") ?? AppName;
232-
Environment = TryGetValue(obj, "Environment") ?? Environment;
262+
Environment = GetEnvironment(obj);
233263
ApiKey = TryGetValue(obj, "ApiKey") ?? ApiKey;
234264
}
235265

@@ -261,5 +291,17 @@ private static string TryGetValue(JToken jToken, string key)
261291

262292
return r;
263293
}
294+
public static string GetEnvironment(JObject envName)
295+
{
296+
var environmentName = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
297+
if (!String.IsNullOrEmpty(environmentName))
298+
{
299+
return environmentName;
300+
}
301+
302+
environmentName = TryGetValue(envName, "Environment") ?? Environment;
303+
304+
return environmentName;
305+
}
264306
}
265307
}

samples/CoreConsoleApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void Main(string[] args)
3838

3939
string filePath = "C:\\Source\\stackify-api-dotnet\\samples\\CoreConsoleApp\\Stackify.json";
4040

41-
Config.ReadStackifyJSONConfig(filePath);
41+
Config.ReadStackifyJSONConfig();
4242

4343
//NLogTest();
4444
Console.WriteLine($"Stackify Config AppName: {StackifyLib.Config.AppName}");

samples/CoreWebApp/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Startup(IHostingEnvironment env)
4141

4242
string filePath = "C:\\Source\\stackify-api-dotnet\\samples\\CoreWebApp\\Stackify.json";
4343

44-
Config.ReadStackifyJSONConfig(filePath);
44+
Config.ReadStackifyJSONConfig();
4545
Debug.WriteLine(StackifyLib.Config.AppName);
4646
Debug.WriteLine(StackifyLib.Config.Environment);
4747
Debug.WriteLine(StackifyLib.Config.ApiKey);

test/StackifyLib.UnitTests/ConfigJSON_Tests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ConfigJSON_Tests(ITestOutputHelper output)
2424
this.output = output;
2525
}
2626

27-
27+
#if JSONTEST
2828
[Fact]
2929
public void Should_Ignore_Invalid_Format_Json_File()
3030
{
@@ -121,5 +121,7 @@ private void ResetConfig()
121121
StackifyLib.Config.Environment = Environment;
122122
StackifyLib.Config.ApiKey = ApiKey;
123123
}
124+
#endif
124125
}
126+
125127
}

0 commit comments

Comments
 (0)