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>
@@ -68,19 +72,16 @@ public static void ReadAllConfig(this ExceptionlessConfiguration config, params
68
72
config . ReadFromAttributes ( configAttributesAssemblies ) ;
69
73
70
74
config . ReadFromConfigSection ( ) ;
75
+ config . ReadFromAppSettings ( ) ;
76
+ config . ReadFromEnvironmentalVariables ( ) ;
71
77
config . ApplySavedServerSettings ( ) ;
72
78
}
73
79
74
80
/// <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 .
76
82
/// </summary>
77
83
/// <param name="config">The configuration object you want to apply the attribute settings to.</param>
78
84
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
85
ExceptionlessSection section = null ;
85
86
86
87
try {
@@ -93,9 +94,8 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
93
94
return ;
94
95
95
96
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 ) )
99
99
config . ApiKey = section . ApiKey ;
100
100
101
101
if ( ! String . IsNullOrEmpty ( section . ServerUrl ) )
@@ -161,6 +161,61 @@ public static void ReadFromConfigSection(this ExceptionlessConfiguration config)
161
161
}
162
162
}
163
163
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
+
164
219
private static bool IsValidApiKey ( string apiKey ) {
165
220
return ! String . IsNullOrEmpty ( apiKey ) && apiKey != "API_KEY_HERE" ;
166
221
}
0 commit comments