1
1
using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
2
4
using System . Configuration ;
3
5
using System . Diagnostics ;
4
6
using System . IO ;
15
17
16
18
namespace Exceptionless {
17
19
public static class ExceptionlessExtraConfigurationExtensions {
20
+ private static Dictionary < string , string > _environmentVariables ;
21
+
18
22
/// <summary>
19
23
/// Reads the Exceptionless configuration from the app.config or web.config file.
20
24
/// </summary>
@@ -72,15 +76,10 @@ public static void ReadAllConfig(this ExceptionlessConfiguration config, params
72
76
}
73
77
74
78
/// <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 .
76
80
/// </summary>
77
81
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
78
82
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
-
84
83
ExceptionlessSection section = null ;
85
84
86
85
try {
@@ -93,9 +92,8 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
93
92
return ;
94
93
95
94
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 ) )
99
97
config . ApiKey = section . ApiKey ;
100
98
101
99
if ( ! String . IsNullOrEmpty ( section . ServerUrl ) )
@@ -161,6 +159,69 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
161
159
}
162
160
}
163
161
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
+
164
225
private static bool IsValidApiKey ( string apiKey ) {
165
226
return ! String . IsNullOrEmpty ( apiKey ) && apiKey != "API_KEY_HERE" ;
166
227
}
0 commit comments