Skip to content

Commit 086009c

Browse files
author
Todd Lair
authored
Merge pull request #119 from homiedopie/hotfix/DOTNET-71
(DOTNET-71) - Add more debug messages for config loading
2 parents 11fd03d + 2093c24 commit 086009c

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

Src/StackifyLib.AspNetCore/Extensions.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@ public static void ConfigureStackifyLogging(this Microsoft.AspNetCore.Builder.IA
1111
try
1212
{
1313
Configure.SubscribeToWebRequestDetails(app.ApplicationServices);
14+
}
15+
catch (Exception ex)
16+
{
17+
Debug.WriteLine($"#Extensions - #AspNetCore - Error Subscribing to WebRequestDetails {ex}");
18+
StackifyLib.Utils.StackifyAPILogger.Log("#Extensions - #AspNetCore - Error Subscribing to WebRequestDetails", ex);
19+
}
1420

15-
if (configuration != null)
21+
try
22+
{
23+
if (configuration == null)
1624
{
17-
StackifyLib.Config.SetConfiguration(configuration);
18-
19-
//tell it to load all the settings since we now have the config
20-
Config.LoadSettings();
25+
StackifyLib.Utils.StackifyAPILogger.Log("#Extensions - #AspNetCore - Empty configuration, ignoring");
26+
return;
2127
}
28+
29+
StackifyLib.Utils.StackifyAPILogger.Log("#Extensions - #AspNetCore - ConfigureStackifyLogging - Initialize");
30+
StackifyLib.Config.SetConfiguration(configuration);
31+
StackifyLib.Utils.StackifyAPILogger.Log("#Extensions - #AspNetCore - ConfigureStackifyLogging - Settings Loaded");
32+
Config.LoadSettings();
2233
}
2334
catch (Exception ex)
2435
{
25-
Debug.WriteLine($"Error in ConfigureStackifyLogging {ex}");
36+
Debug.WriteLine($"#Extensions - #AspNetCore - Error in ConfigureStackifyLogging {ex}");
37+
StackifyLib.Utils.StackifyAPILogger.Log("#Extensions - #AspNetCore - Error in ConfigureStackifyLogging", ex);
2638
}
2739
}
2840
}

Src/StackifyLib/Config.cs

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public static void LoadSettings()
4141
ReadStackifyJSONConfig(); // TODO: Better way?
4242
}
4343

44+
if (IsStackifyJsonLoaded == true)
45+
{
46+
StackifyAPILogger.Log($"#Config - LoadSettings - Stackify JSON Loaded");
47+
}
48+
49+
#if NETCORE || NETCOREX
50+
if (_configuration != null)
51+
{
52+
StackifyAPILogger.Log($"#Config - LoadSettings - Configuration Set");
53+
}
54+
#endif
55+
4456
CaptureServerVariables = Get("Stackify.CaptureServerVariables", bool.FalseString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
4557

4658
CaptureSessionVariables = Get("Stackify.CaptureSessionVariables", bool.FalseString).Equals(bool.TrueString, StringComparison.CurrentCultureIgnoreCase);
@@ -134,7 +146,7 @@ public static void LoadSettings()
134146
}
135147

136148
var rumKey = Get("Stackify.Rum_Key");
137-
if (Regex.IsMatch(rumKey, "^[A-Za-z0-9_-]+$"))
149+
if (string.IsNullOrWhiteSpace(rumKey) == false && Regex.IsMatch(rumKey, "^[A-Za-z0-9_-]+$"))
138150
{
139151
RumKey = rumKey;
140152
}
@@ -197,6 +209,7 @@ public static void LoadSettings()
197209
internal static string Get(string key, string defaultValue = null)
198210
{
199211
string v = null;
212+
string section = null;
200213

201214
try
202215
{
@@ -207,55 +220,69 @@ internal static string Get(string key, string defaultValue = null)
207220
{
208221
var appSettings = _configuration.GetSection("Stackify");
209222
v = appSettings[key.Replace("Stackify.", string.Empty)];
223+
section = "ConfigStackify";
210224

211225
//Get settings from Stackify.json
212-
if (string.IsNullOrEmpty(v))
226+
if (string.IsNullOrWhiteSpace(v))
213227
{
214228
var key2 = key.Replace("Stackify.", string.Empty);
215229
var stackifyJson = _configuration.GetSection(key2);
216230
v = stackifyJson.Value;
217-
if (string.IsNullOrEmpty(v))
231+
section = "ConfigDirect";
232+
233+
if (string.IsNullOrWhiteSpace(v))
218234
{
219235
// Search in Retrace, but key will likely still be Stackify.name, not Retrace.name in the code
220236
var retraceAppSettings = _configuration.GetSection("Retrace");
221237
v = retraceAppSettings[key.Replace("Stackify.", string.Empty)];
238+
section = "ConfigRetrace";
222239
}
223240
}
224241
}
225242
#endif
226243

227244
#if NETFULL
228-
if (string.IsNullOrEmpty(v))
229-
{
230-
v = System.Configuration.ConfigurationManager.AppSettings[key];
231-
}
245+
if (string.IsNullOrWhiteSpace(v))
246+
{
247+
v = System.Configuration.ConfigurationManager.AppSettings[key];
248+
section = "AppSettings";
249+
}
232250
#endif
233251

234-
if (string.IsNullOrEmpty(v))
235-
{
236-
v = System.Environment.GetEnvironmentVariable(key);
237-
}
252+
if (string.IsNullOrWhiteSpace(v))
253+
{
254+
v = System.Environment.GetEnvironmentVariable(key);
255+
section = "Env";
256+
}
238257

239-
if (string.IsNullOrEmpty(v))
240-
{
241-
v = System.Environment.GetEnvironmentVariable(key.ToUpperInvariant());
242-
}
258+
if (string.IsNullOrWhiteSpace(v))
259+
{
260+
v = System.Environment.GetEnvironmentVariable(key.ToUpperInvariant());
261+
section = "EnvUpper";
262+
}
243263

244-
if (string.IsNullOrEmpty(v))
245-
{
246-
// Linux systems do not allow period in an environment variable name
247-
v = System.Environment.GetEnvironmentVariable(key.Replace('.', '_').ToUpperInvariant());
248-
}
264+
if (string.IsNullOrWhiteSpace(v))
265+
{
266+
// Linux systems do not allow period in an environment variable name
267+
v = System.Environment.GetEnvironmentVariable(key.Replace('.', '_').ToUpperInvariant());
268+
section = "EnvUpperLinux";
269+
}
249270

250-
if (string.IsNullOrEmpty(v) && key.StartsWith("Stackify."))
251-
{
252-
v = System.Environment.GetEnvironmentVariable("RETRACE_" + key.Substring(9).Replace('.', '_').ToUpperInvariant());
253-
}
271+
if (string.IsNullOrWhiteSpace(v) && key.StartsWith("Stackify."))
272+
{
273+
v = System.Environment.GetEnvironmentVariable("RETRACE_" + key.Substring(9).Replace('.', '_').ToUpperInvariant());
274+
section = "EnvUpperLinuxRetrace";
275+
}
276+
277+
if (string.IsNullOrWhiteSpace(v) == false)
278+
{
279+
StackifyAPILogger.Log($"#Config - #Get - Section: {section} - Key: {key} - Value: {v ?? "Empty"}");
280+
}
254281
}
255282
}
256-
catch (Exception ex)
283+
catch (Exception ex)
257284
{
258-
StackifyAPILogger.Log("#Config #Get failed", ex);
285+
StackifyAPILogger.Log("#Config - #Get - #Failed", ex);
259286
}
260287
finally
261288
{
@@ -272,19 +299,17 @@ public static void ReadStackifyJSONConfig()
272299
{
273300
try
274301
{
275-
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
302+
var ASPEnvironment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
276303
var DotnetEnvironment = System.Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
277304
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
278305
string jsonPath = string.Empty;
279306
string json = string.Empty;
280307

281-
ASPEnvironment = "Production";
282-
283-
if (!String.IsNullOrEmpty(ASPEnvironment))
308+
if (!String.IsNullOrWhiteSpace(ASPEnvironment))
284309
{
285310
jsonPath = Path.Combine(baseDirectory, $"Stackify.{ASPEnvironment}.json");
286311
}
287-
else if (!String.IsNullOrEmpty(DotnetEnvironment))
312+
else if (!String.IsNullOrWhiteSpace(DotnetEnvironment))
288313
{
289314
jsonPath = Path.Combine(baseDirectory, $"Stackify.{DotnetEnvironment}.json");
290315
}
@@ -312,11 +337,11 @@ public static void ReadStackifyJSONConfig()
312337
string iisBaseDirectory = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
313338
string iisJsonPath = string.Empty;
314339

315-
if (!String.IsNullOrEmpty(ASPEnvironment))
340+
if (!String.IsNullOrWhiteSpace(ASPEnvironment))
316341
{
317342
iisJsonPath = Path.Combine(iisBaseDirectory, $"Stackify.{ASPEnvironment}.json");
318343
}
319-
else if (!String.IsNullOrEmpty(DotnetEnvironment))
344+
else if (!String.IsNullOrWhiteSpace(DotnetEnvironment))
320345
{
321346
iisJsonPath = Path.Combine(iisBaseDirectory, $"Stackify.{DotnetEnvironment}.json");
322347
}

Src/StackifyLib/Extensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ public static StackifyLib.StackifyError NewStackifyError(this Exception ex)
3434
#if NETCORE || NETCOREX
3535
public static void ConfigureStackifyLogging(this Microsoft.Extensions.Configuration.IConfiguration configuration)
3636
{
37+
StackifyAPILogger.Log("#Extensions - ConfigureStackifyLogging - Initialize");
3738
Config.SetConfiguration(configuration);
3839
//tell it to load all the settings since we now have the config
3940
Config.LoadSettings();
41+
StackifyAPILogger.Log("#Extensions - ConfigureStackifyLogging - Settings Loaded");
4042
}
4143

4244
public static void ConfigureStackifyLib(this Microsoft.Extensions.Configuration.IConfiguration configuration)
4345
{
46+
StackifyAPILogger.Log("#Extensions - ConfigureStackifyLib - Initialize");
4447
Config.SetConfiguration(configuration);
4548
//tell it to load all the settings since we now have the config
4649
Config.LoadSettings();
50+
StackifyAPILogger.Log("#Extensions - ConfigureStackifyLib - Settings Loaded");
4751
}
4852
#endif
4953
}

0 commit comments

Comments
 (0)