Skip to content

Commit af0ff39

Browse files
authored
Added .NET Core config support for EnableLogging , LogPath, QueueMaxAge, QueueMaxAttempts,, StorageSerializer (#237)
1 parent f2267fc commit af0ff39

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Reflection;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Http;
67
using Exceptionless.AspNetCore;
8+
using Exceptionless.Dependency;
9+
using Exceptionless.Logging;
710
using Exceptionless.Models;
811
using Exceptionless.Models.Data;
912
using Exceptionless.Plugins.Default;
13+
using Exceptionless.Serializer;
1014
using Exceptionless.Storage;
1115
using Microsoft.AspNetCore.Hosting;
1216
using Microsoft.Extensions.Configuration;
@@ -59,35 +63,64 @@ public static void ReadFromConfiguration(this ExceptionlessConfiguration config,
5963
throw new ArgumentNullException(nameof(settings));
6064

6165
var section = settings.GetSection("Exceptionless");
62-
66+
if (Boolean.TryParse(section["Enabled"], out bool enabled) && !enabled)
67+
config.Enabled = false;
68+
6369
string apiKey = section["ApiKey"];
6470
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
6571
config.ApiKey = apiKey;
6672

67-
foreach (var data in section.GetSection("DefaultData").GetChildren())
68-
if (data.Value != null)
69-
config.DefaultData[data.Key] = data.Value;
70-
71-
foreach (var tag in section.GetSection("DefaultTags").GetChildren())
72-
config.DefaultTags.Add(tag.Value);
73-
74-
if (Boolean.TryParse(section["Enabled"], out bool enabled) && !enabled)
75-
config.Enabled = false;
76-
77-
if (Boolean.TryParse(section["IncludePrivateInformation"], out bool includePrivateInformation) && !includePrivateInformation)
78-
config.IncludePrivateInformation = false;
79-
8073
string serverUrl = section["ServerUrl"];
8174
if (!String.IsNullOrEmpty(serverUrl))
8275
config.ServerUrl = serverUrl;
76+
77+
if (TimeSpan.TryParse(section["QueueMaxAge"], out var queueMaxAge))
78+
config.QueueMaxAge = queueMaxAge;
8379

80+
if (Int32.TryParse(section["QueueMaxAttempts"], out int queueMaxAttempts))
81+
config.QueueMaxAttempts = queueMaxAttempts;
82+
8483
string storagePath = section["StoragePath"];
8584
if (!String.IsNullOrEmpty(storagePath))
8685
config.Resolver.Register(typeof(IObjectStorage), () => new FolderObjectStorage(config.Resolver, storagePath));
8786

87+
string storageSerializer = section["StorageSerializer"];
88+
if (!String.IsNullOrEmpty(storageSerializer)) {
89+
try {
90+
var serializerType = Type.GetType(storageSerializer);
91+
if (!typeof(IStorageSerializer).GetTypeInfo().IsAssignableFrom(serializerType)) {
92+
config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), $"The storage serializer {storageSerializer} does not implemented interface {typeof(IStorageSerializer)}.");
93+
} else {
94+
config.Resolver.Register(typeof(IStorageSerializer), serializerType);
95+
}
96+
} catch (Exception ex) {
97+
config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), ex, $"The storage serializer {storageSerializer} type could not be resolved: ${ex.Message}");
98+
}
99+
}
100+
101+
if (Boolean.TryParse(section["EnableLogging"], out bool enableLogging) && enableLogging) {
102+
string logPath = section["LogPath"];
103+
if (!String.IsNullOrEmpty(logPath))
104+
config.UseFileLogger(logPath);
105+
else if (!String.IsNullOrEmpty(storagePath))
106+
config.UseFileLogger(System.IO.Path.Combine(storagePath, "exceptionless.log"));
107+
}
108+
109+
if (Boolean.TryParse(section["IncludePrivateInformation"], out bool includePrivateInformation) && !includePrivateInformation)
110+
config.IncludePrivateInformation = false;
111+
112+
foreach (var tag in section.GetSection("DefaultTags").GetChildren())
113+
config.DefaultTags.Add(tag.Value);
114+
115+
foreach (var data in section.GetSection("DefaultData").GetChildren())
116+
if (data.Value != null)
117+
config.DefaultData[data.Key] = data.Value;
118+
88119
foreach (var setting in section.GetSection("Settings").GetChildren())
89120
if (setting.Value != null)
90121
config.Settings[setting.Key] = setting.Value;
122+
123+
// TODO: Support Registrations
91124
}
92125

93126
/// <summary>

0 commit comments

Comments
 (0)