Skip to content

Commit abebcb6

Browse files
authored
Merge pull request #120 from homiedopie/feature/DOTNET-89
DOTNET-89 - Improve logger and config loading and environment detail
2 parents e3b6745 + 5b9922b commit abebcb6

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

Src/StackifyLib/Config.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static void LoadSettings()
3838
try
3939
{
4040
if (IsStackifyJsonLoaded == false) {
41+
StackifyAPILogger.Log($"#Config - LoadSettings - Stackify JSON - Read");
4142
ReadStackifyJSONConfig(); // TODO: Better way?
4243
}
4344

@@ -155,6 +156,15 @@ public static void LoadSettings()
155156
{
156157
StackifyAPILogger.Log("#Config #LoadSettings failed", ex);
157158
}
159+
160+
try
161+
{
162+
StackifyAPILogger.EvaluateLogEnabled();
163+
}
164+
catch (Exception ex)
165+
{
166+
StackifyAPILogger.Log("#Config #LoadSettings StackifyAPILogger.EvaluateLogEnabled failed", ex);
167+
}
158168
}
159169

160170
public static string ApiKey { get; set; }
@@ -299,7 +309,7 @@ public static void ReadStackifyJSONConfig()
299309
{
300310
try
301311
{
302-
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
312+
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
303313
var DotnetEnvironment = System.Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
304314
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
305315
string jsonPath = string.Empty;
@@ -318,6 +328,7 @@ public static void ReadStackifyJSONConfig()
318328
jsonPath = Path.Combine(baseDirectory, "Stackify.json");
319329
}
320330

331+
StackifyAPILogger.Log($"#Config #Json Load - Path: {jsonPath}");
321332
if (File.Exists(jsonPath))
322333
{
323334
using (var fs = new FileStream(jsonPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@@ -350,6 +361,7 @@ public static void ReadStackifyJSONConfig()
350361
iisJsonPath = Path.Combine(iisBaseDirectory, "Stackify.json");
351362
}
352363

364+
StackifyAPILogger.Log($"#Config #Json Load - Path: {iisJsonPath}");
353365
if (File.Exists(iisJsonPath))
354366
{
355367
using (var fs = new FileStream(iisJsonPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@@ -427,6 +439,7 @@ public static void SetStackifyObj(JObject obj)
427439
AppName = TryGetValue(obj, "AppName") ?? AppName;
428440
Environment = TryGetValue(obj, "Environment") ?? Environment;
429441
ApiKey = TryGetValue(obj, "ApiKey") ?? ApiKey;
442+
StackifyAPILogger.Log($"#Config #Json #SetStackifyObj Load - AppName: {AppName} - Environment: {Environment} - ApiKey: {ApiKey}");
430443
}
431444

432445
private static string TryGetValue(JToken jToken, string key)
@@ -449,6 +462,7 @@ private static string TryGetValue(JToken jToken, string key)
449462
}
450463

451464
r = val.ToString();
465+
StackifyAPILogger.Log($"#Config #TryGetValue #Json Success - Key: {key} - Value: {r ?? "Empty"}");
452466
}
453467
catch (Exception ex)
454468
{

Src/StackifyLib/Models/EnvironmentDetail.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,5 +490,37 @@ public string AppNameToUse()
490490

491491
return AppName;
492492
}
493+
494+
public void UpdateAppName()
495+
{
496+
if (!string.IsNullOrEmpty(ConfiguredAppName))
497+
{
498+
return;
499+
}
500+
501+
if (!string.IsNullOrEmpty(Config.AppName))
502+
{
503+
ConfiguredAppName = Config.AppName;
504+
return;
505+
}
506+
507+
ConfiguredAppName = Config.Get("Stackify.AppName");
508+
}
509+
510+
public void UpdateEnvironmentName()
511+
{
512+
if (!string.IsNullOrEmpty(ConfiguredEnvironmentName))
513+
{
514+
return;
515+
}
516+
517+
if (!string.IsNullOrEmpty(Config.Environment))
518+
{
519+
ConfiguredEnvironmentName = Config.Environment;
520+
return;
521+
}
522+
523+
ConfiguredEnvironmentName = Config.Get("Stackify.Environment");
524+
}
493525
}
494526
}

Src/StackifyLib/Utils/HttpClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ public bool IdentifyApp()
302302
}
303303
StackifyAPILogger.Log("Calling to Identify App");
304304
EnvironmentDetail env = EnvironmentDetail.Get();
305+
env.UpdateEnvironmentName();
306+
env.UpdateAppName();
307+
if (string.IsNullOrEmpty(env.ConfiguredAppName) && !string.IsNullOrEmpty(Config.AppName)) {
308+
env.ConfiguredAppName = Config.AppName;
309+
}
310+
311+
if (string.IsNullOrEmpty(env.ConfiguredEnvironmentName) && !string.IsNullOrEmpty(Config.Environment)) {
312+
env.ConfiguredAppName = Config.Environment;
313+
}
314+
305315
string jsonData = JsonConvert.SerializeObject(env, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
306316

307317
var response =

Src/StackifyLib/Utils/StackifyAPILogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class StackifyAPILogger
1919

2020
static StackifyAPILogger()
2121
{
22-
EvaluateLogEnabled();
2322
}
2423

2524
public static bool LogEnabled
@@ -85,7 +84,7 @@ public static void Log(string message, Exception ex, bool logAnyways = false)
8584
}
8685
}
8786

88-
private static void EvaluateLogEnabled()
87+
public static void EvaluateLogEnabled()
8988
{
9089
if (_logEnabled == null)
9190
{

0 commit comments

Comments
 (0)