Skip to content

Commit d772b55

Browse files
committed
Updates to #37 Added the ability to read settings from environmental variables.
1 parent c93d6c1 commit d772b55

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

Source/Extras/Extensions/ExceptionlessExtraConfigurationExtensions.cs

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Configuration;
35
using System.Diagnostics;
46
using System.IO;
@@ -15,6 +17,8 @@
1517

1618
namespace Exceptionless {
1719
public static class ExceptionlessExtraConfigurationExtensions {
20+
private static Dictionary<string, string> _environmentVariables;
21+
1822
/// <summary>
1923
/// Reads the Exceptionless configuration from the app.config or web.config file.
2024
/// </summary>
@@ -72,15 +76,10 @@ public static void ReadAllConfig(this ExceptionlessConfiguration config, params
7276
}
7377

7478
/// <summary>
75-
/// Reads the Exceptionless configuration from the app.config or web.config file.
79+
/// Reads the Exceptionless configuration from the app.config or web.config files configuration section.
7680
/// </summary>
7781
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
7882
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-
8483
ExceptionlessSection section = null;
8584

8685
try {
@@ -93,9 +92,8 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
9392
return;
9493

9594
config.Enabled = section.Enabled;
96-
97-
// Only update if it hasn't already been set via app settings.
98-
if (!IsValidApiKey(apiKeyOverride) && IsValidApiKey(section.ApiKey))
95+
96+
if (IsValidApiKey(section.ApiKey))
9997
config.ApiKey = section.ApiKey;
10098

10199
if (!String.IsNullOrEmpty(section.ServerUrl))
@@ -161,6 +159,69 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
161159
}
162160
}
163161

162+
/// <summary>
163+
/// Reads the Exceptionless configuration from the app.config or web.config files app settings.
164+
/// </summary>
165+
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
166+
public static void ReadFromAppSettings(this ExceptionlessConfiguration config) {
167+
string apiKey = ConfigurationManager.AppSettings["Exceptionless:ApiKey"];
168+
if (IsValidApiKey(apiKey))
169+
config.ApiKey = apiKey;
170+
171+
bool enabled;
172+
if (Boolean.TryParse(ConfigurationManager.AppSettings["Exceptionless:Enabled"], out enabled))
173+
config.Enabled = enabled;
174+
175+
bool enableSSL;
176+
if (Boolean.TryParse(ConfigurationManager.AppSettings["Exceptionless:EnableSSL"], out enableSSL))
177+
config.EnableSSL = enableSSL;
178+
179+
string serverUrl = ConfigurationManager.AppSettings["Exceptionless:ServerUrl"];
180+
if (!String.IsNullOrEmpty(serverUrl))
181+
config.ApiKey = serverUrl;
182+
}
183+
184+
/// <summary>
185+
/// Reads the Exceptionless configuration from Environment Variables.
186+
/// </summary>
187+
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
188+
public static void ReadFromEnvironmentalVariables(this ExceptionlessConfiguration config) {
189+
string apiKey = GetEnvironmentalVariable("Exceptionless:ApiKey");
190+
if (IsValidApiKey(apiKey))
191+
config.ApiKey = apiKey;
192+
193+
bool enabled;
194+
if (Boolean.TryParse(GetEnvironmentalVariable("Exceptionless:Enabled"), out enabled))
195+
config.Enabled = enabled;
196+
197+
bool enableSSL;
198+
if (Boolean.TryParse(GetEnvironmentalVariable("Exceptionless:EnableSSL"), out enableSSL))
199+
config.EnableSSL = enableSSL;
200+
201+
string serverUrl = GetEnvironmentalVariable("Exceptionless:ServerUrl");
202+
if (!String.IsNullOrEmpty(serverUrl))
203+
config.ApiKey = serverUrl;
204+
}
205+
206+
private static string GetEnvironmentalVariable(string name) {
207+
if (String.IsNullOrEmpty(name))
208+
return null;
209+
210+
if (_environmentVariables == null) {
211+
try {
212+
_environmentVariables = Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().ToDictionary(e => e.Key.ToString(), e => e.Value.ToString());
213+
} catch (Exception ex) {
214+
_environmentVariables = new Dictionary<string, string>();
215+
return null;
216+
}
217+
}
218+
219+
if (!_environmentVariables.ContainsKey(name))
220+
return null;
221+
222+
return _environmentVariables[name];
223+
}
224+
164225
private static bool IsValidApiKey(string apiKey) {
165226
return !String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE";
166227
}

0 commit comments

Comments
 (0)