Skip to content

Commit e9fdaf4

Browse files
committed
Merge pull request #38 from exceptionless/environment-variables
Pull request for #37 Added the ability to read settings from environmental variables.
2 parents c93d6c1 + b4ea381 commit e9fdaf4

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

Source/Extras/Extensions/ExceptionlessExtraConfigurationExtensions.cs

Lines changed: 64 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>
@@ -68,19 +72,16 @@ public static void ReadAllConfig(this ExceptionlessConfiguration config, params
6872
config.ReadFromAttributes(configAttributesAssemblies);
6973

7074
config.ReadFromConfigSection();
75+
config.ReadFromAppSettings();
76+
config.ReadFromEnvironmentalVariables();
7177
config.ApplySavedServerSettings();
7278
}
7379

7480
/// <summary>
75-
/// Reads the Exceptionless configuration from the app.config or web.config file.
81+
/// Reads the Exceptionless configuration from the app.config or web.config files configuration section.
7682
/// </summary>
7783
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
7884
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-
8485
ExceptionlessSection section = null;
8586

8687
try {
@@ -93,9 +94,8 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
9394
return;
9495

9596
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))
97+
98+
if (IsValidApiKey(section.ApiKey))
9999
config.ApiKey = section.ApiKey;
100100

101101
if (!String.IsNullOrEmpty(section.ServerUrl))
@@ -161,6 +161,61 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
161161
}
162162
}
163163

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

Source/Shared/Configuration/ExceptionlessAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public ExceptionlessAttribute(string apiKey) {
3232
/// </summary>
3333
/// ///
3434
/// <value><c>true</c> to enable SSL; otherwise, <c>false</c>.</value>
35+
[ObsoleteAttribute("This property will be removed in a future release.")]
3536
public bool EnableSSL { get; set; }
3637

3738
/// <summary>

Source/Shared/Configuration/ExceptionlessConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public string ApiKey {
9797
/// <summary>
9898
/// Whether or not the client should use SSL when communicating with the server.
9999
/// </summary>
100+
[ObsoleteAttribute("This property will be removed in a future release.")]
100101
public bool EnableSSL { get; set; }
101102

102103
/// <summary>

0 commit comments

Comments
 (0)