Skip to content

Commit da1f9f3

Browse files
committed
Updates to #22 Read the api key from app settings regardless if there is a config section.
@ejsmith A end user ran into an issue where they had a config transform that set the api key and then they were using an azure app setting. When they did this they occasionally ran into an issue with the api key already set. I wonder if there is a better way to handle this.. even for attribute definitions..
1 parent 366ecd8 commit da1f9f3

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Source/Extras/Extensions/ExceptionlessExtraConfigurationExtensions.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public static void ReadAllConfig(this ExceptionlessConfiguration config, params
7676
/// </summary>
7777
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
7878
public static void ReadFromConfigSection(this ExceptionlessConfiguration config) {
79+
// If an appsetting is present for ApiKey, then it will override the other api keys
80+
string apiKeyOverride = ConfigurationManager.AppSettings["Exceptionless:ApiKey"];
81+
if (IsValidApiKey(apiKeyOverride))
82+
config.ApiKey = apiKeyOverride;
83+
7984
ExceptionlessSection section = null;
8085

8186
try {
@@ -89,15 +94,10 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
8994

9095
config.Enabled = section.Enabled;
9196

92-
// Only update if it is not null
93-
if (!String.IsNullOrEmpty(section.ApiKey) && section.ApiKey != "API_KEY_HERE")
97+
// Only update if it hasn't already been set via app settings.
98+
if (!IsValidApiKey(apiKeyOverride) && IsValidApiKey(section.ApiKey))
9499
config.ApiKey = section.ApiKey;
95-
96-
// If an appsetting is present for ApiKey, then it will override the other api keys
97-
string apiKeyOverride = ConfigurationManager.AppSettings["Exceptionless:ApiKey"] ?? String.Empty;
98-
if (!String.IsNullOrEmpty(apiKeyOverride) && apiKeyOverride != "API_KEY_HERE")
99-
config.ApiKey = apiKeyOverride;
100-
100+
101101
if (!String.IsNullOrEmpty(section.ServerUrl))
102102
config.ServerUrl = section.ServerUrl;
103103

@@ -160,5 +160,9 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
160160
}
161161
}
162162
}
163+
164+
private static bool IsValidApiKey(string apiKey) {
165+
return !String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE";
166+
}
163167
}
164168
}

0 commit comments

Comments
 (0)