Skip to content

Commit 092ec02

Browse files
authored
Add support for IConfiguration when using netstandard. (#307)
1 parent 3927be6 commit 092ec02

File tree

3 files changed

+85
-81
lines changed

3 files changed

+85
-81
lines changed

src/Exceptionless/Exceptionless.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
</PropertyGroup>
3535

3636
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'" Label="Package References">
37+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
3738
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
3839
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
3940
</ItemGroup>

src/Exceptionless/Extensions/ExceptionlessConfigurationExtensions.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
using Exceptionless.Storage;
1616
using Exceptionless.Diagnostics;
1717

18+
#if NETSTANDARD
19+
using Microsoft.Extensions.Configuration;
20+
#endif
21+
1822
#if NET45
1923
using System.Configuration;
2024
using Exceptionless.Extensions;
@@ -389,6 +393,86 @@ public static void ReadFromAppSettings(this ExceptionlessConfiguration config) {
389393
}
390394
#endif
391395

396+
#if NETSTANDARD
397+
/// <summary>
398+
/// Sets the configuration from .net configuration settings.
399+
/// </summary>
400+
/// <param name="config">The configuration object you want to apply the settings to.</param>
401+
/// <param name="settings">The configuration settings</param>
402+
public static void ReadFromConfiguration(this ExceptionlessConfiguration config, IConfiguration settings) {
403+
if (config == null)
404+
throw new ArgumentNullException(nameof(config));
405+
406+
if (settings == null)
407+
throw new ArgumentNullException(nameof(settings));
408+
409+
var section = settings.GetSection("Exceptionless");
410+
if (Boolean.TryParse(section["Enabled"], out bool enabled) && !enabled)
411+
config.Enabled = false;
412+
413+
string apiKey = section["ApiKey"];
414+
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
415+
config.ApiKey = apiKey;
416+
417+
string serverUrl = section["ServerUrl"];
418+
if (!String.IsNullOrEmpty(serverUrl))
419+
config.ServerUrl = serverUrl;
420+
421+
if (TimeSpan.TryParse(section["QueueMaxAge"], out var queueMaxAge))
422+
config.QueueMaxAge = queueMaxAge;
423+
424+
if (Int32.TryParse(section["QueueMaxAttempts"], out int queueMaxAttempts))
425+
config.QueueMaxAttempts = queueMaxAttempts;
426+
427+
string storagePath = section["StoragePath"];
428+
if (!String.IsNullOrEmpty(storagePath))
429+
config.Resolver.Register(typeof(IObjectStorage), () => new FolderObjectStorage(config.Resolver, storagePath));
430+
431+
string storageSerializer = section["StorageSerializer"];
432+
if (!String.IsNullOrEmpty(storageSerializer)) {
433+
try {
434+
var serializerType = Type.GetType(storageSerializer);
435+
if (!typeof(IStorageSerializer).GetTypeInfo().IsAssignableFrom(serializerType)) {
436+
config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), $"The storage serializer {storageSerializer} does not implemented interface {typeof(IStorageSerializer)}.");
437+
}
438+
else {
439+
config.Resolver.Register(typeof(IStorageSerializer), serializerType);
440+
}
441+
}
442+
catch (Exception ex) {
443+
config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), ex, $"The storage serializer {storageSerializer} type could not be resolved: ${ex.Message}");
444+
}
445+
}
446+
447+
if (Boolean.TryParse(section["EnableLogging"], out bool enableLogging) && enableLogging) {
448+
string logPath = section["LogPath"];
449+
if (!String.IsNullOrEmpty(logPath))
450+
config.UseFileLogger(logPath);
451+
else if (!String.IsNullOrEmpty(storagePath))
452+
config.UseFileLogger(System.IO.Path.Combine(storagePath, "exceptionless.log"));
453+
}
454+
455+
if (Boolean.TryParse(section["IncludePrivateInformation"], out bool includePrivateInformation) && !includePrivateInformation)
456+
config.IncludePrivateInformation = false;
457+
458+
if (Boolean.TryParse(section["ProcessQueueOnCompletedRequest"], out bool processQueueOnCompletedRequest) && processQueueOnCompletedRequest)
459+
config.ProcessQueueOnCompletedRequest = true;
460+
461+
foreach (var tag in section.GetSection("DefaultTags").GetChildren())
462+
config.DefaultTags.Add(tag.Value);
463+
464+
foreach (var data in section.GetSection("DefaultData").GetChildren())
465+
if (data.Value != null)
466+
config.DefaultData[data.Key] = data.Value;
467+
468+
foreach (var setting in section.GetSection("Settings").GetChildren())
469+
if (setting.Value != null)
470+
config.Settings[setting.Key] = setting.Value;
471+
472+
// TODO: Support Registrations
473+
}
474+
#endif
475+
392476
/// <summary>
393477
/// Reads the Exceptionless configuration from Environment Variables.
394478
/// </summary>
Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using System;
2-
using System.Reflection;
3-
using Exceptionless.Dependency;
42
using Exceptionless.Extensions.Hosting;
5-
using Exceptionless.Logging;
6-
using Exceptionless.Serializer;
7-
using Exceptionless.Storage;
83
using Microsoft.Extensions.Configuration;
94
using Microsoft.Extensions.DependencyInjection;
105
using Microsoft.Extensions.Hosting;
@@ -83,81 +78,5 @@ public static IServiceCollection AddExceptionless(this IServiceCollection servic
8378
return client;
8479
});
8580
}
86-
87-
/// <summary>
88-
/// Sets the configuration from .net configuration settings.
89-
/// </summary>
90-
/// <param name="config">The configuration object you want to apply the settings to.</param>
91-
/// <param name="settings">The configuration settings</param>
92-
public static void ReadFromConfiguration(this ExceptionlessConfiguration config, IConfiguration settings) {
93-
if (config == null)
94-
throw new ArgumentNullException(nameof(config));
95-
96-
if (settings == null)
97-
throw new ArgumentNullException(nameof(settings));
98-
99-
var section = settings.GetSection("Exceptionless");
100-
if (Boolean.TryParse(section["Enabled"], out bool enabled) && !enabled)
101-
config.Enabled = false;
102-
103-
string apiKey = section["ApiKey"];
104-
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
105-
config.ApiKey = apiKey;
106-
107-
string serverUrl = section["ServerUrl"];
108-
if (!String.IsNullOrEmpty(serverUrl))
109-
config.ServerUrl = serverUrl;
110-
111-
if (TimeSpan.TryParse(section["QueueMaxAge"], out var queueMaxAge))
112-
config.QueueMaxAge = queueMaxAge;
113-
114-
if (Int32.TryParse(section["QueueMaxAttempts"], out int queueMaxAttempts))
115-
config.QueueMaxAttempts = queueMaxAttempts;
116-
117-
string storagePath = section["StoragePath"];
118-
if (!String.IsNullOrEmpty(storagePath))
119-
config.Resolver.Register(typeof(IObjectStorage), () => new FolderObjectStorage(config.Resolver, storagePath));
120-
121-
string storageSerializer = section["StorageSerializer"];
122-
if (!String.IsNullOrEmpty(storageSerializer)) {
123-
try {
124-
var serializerType = Type.GetType(storageSerializer);
125-
if (!typeof(IStorageSerializer).GetTypeInfo().IsAssignableFrom(serializerType)) {
126-
config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), $"The storage serializer {storageSerializer} does not implemented interface {typeof(IStorageSerializer)}.");
127-
} else {
128-
config.Resolver.Register(typeof(IStorageSerializer), serializerType);
129-
}
130-
} catch (Exception ex) {
131-
config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), ex, $"The storage serializer {storageSerializer} type could not be resolved: ${ex.Message}");
132-
}
133-
}
134-
135-
if (Boolean.TryParse(section["EnableLogging"], out bool enableLogging) && enableLogging) {
136-
string logPath = section["LogPath"];
137-
if (!String.IsNullOrEmpty(logPath))
138-
config.UseFileLogger(logPath);
139-
else if (!String.IsNullOrEmpty(storagePath))
140-
config.UseFileLogger(System.IO.Path.Combine(storagePath, "exceptionless.log"));
141-
}
142-
143-
if (Boolean.TryParse(section["IncludePrivateInformation"], out bool includePrivateInformation) && !includePrivateInformation)
144-
config.IncludePrivateInformation = false;
145-
146-
if (Boolean.TryParse(section["ProcessQueueOnCompletedRequest"], out bool processQueueOnCompletedRequest) && processQueueOnCompletedRequest)
147-
config.ProcessQueueOnCompletedRequest = true;
148-
149-
foreach (var tag in section.GetSection("DefaultTags").GetChildren())
150-
config.DefaultTags.Add(tag.Value);
151-
152-
foreach (var data in section.GetSection("DefaultData").GetChildren())
153-
if (data.Value != null)
154-
config.DefaultData[data.Key] = data.Value;
155-
156-
foreach (var setting in section.GetSection("Settings").GetChildren())
157-
if (setting.Value != null)
158-
config.Settings[setting.Key] = setting.Value;
159-
160-
// TODO: Support Registrations
161-
}
16281
}
16382
}

0 commit comments

Comments
 (0)