/// <summary> /// Reads the Exceptionless configuration from the app.config or web.config files configuration section. /// </summary> /// <param name="config">The configuration object you want to apply the attribute settings to.</param> public static void ReadFromConfigSection(this ExceptionlessConfiguration config) { ExceptionlessSection section = null; try { section = ConfigurationManager.GetSection("exceptionless") as ExceptionlessSection; } catch (Exception ex) { config.Resolver.GetLog().Error(typeof(ExceptionlessExtraConfigurationExtensions), ex, String.Concat("An error occurred while retrieving the configuration section. Exception: ", ex.Message)); } if (section == null) { return; } config.Enabled = section.Enabled; if (IsValidApiKey(section.ApiKey)) { config.ApiKey = section.ApiKey; } if (!String.IsNullOrEmpty(section.ServerUrl)) { config.ServerUrl = section.ServerUrl; } if (section.EnableSSL.HasValue) { config.EnableSSL = section.EnableSSL.Value; } if (section.QueueMaxAge.HasValue) { config.QueueMaxAge = section.QueueMaxAge.Value; } if (section.QueueMaxAttempts.HasValue) { config.QueueMaxAttempts = section.QueueMaxAttempts.Value; } if (!String.IsNullOrEmpty(section.StoragePath)) { config.UseFolderStorage(section.StoragePath); } if (section.EnableLogging.HasValue && section.EnableLogging.Value) { if (!String.IsNullOrEmpty(section.LogPath)) { config.UseFileLogger(section.LogPath); } else if (!String.IsNullOrEmpty(section.StoragePath)) { config.UseFileLogger(Path.Combine(section.StoragePath, "exceptionless.log")); } else if (!config.Resolver.HasRegistration <IExceptionlessLog>()) { config.UseIsolatedStorageLogger(); } } foreach (var tag in section.Tags.SplitAndTrim(',').Where(tag => !String.IsNullOrEmpty(tag))) { config.DefaultTags.Add(tag); } if (section.ExtendedData != null) { foreach (NameValueConfigurationElement setting in section.ExtendedData) { if (!String.IsNullOrEmpty(setting.Name)) { config.DefaultData[setting.Name] = setting.Value; } } } if (section.Settings != null) { foreach (NameValueConfigurationElement setting in section.Settings) { if (!String.IsNullOrEmpty(setting.Name)) { config.Settings[setting.Name] = setting.Value; } } } if (section.Registrations != null && section.Registrations.Count > 0) { var types = AssemblyHelper.GetTypes(config.Resolver.GetLog()); foreach (RegistrationConfigElement resolver in section.Registrations) { if (String.IsNullOrEmpty(resolver.Service) || String.IsNullOrEmpty(resolver.Type)) { continue; } Type resolverInterface = types.FirstOrDefault(t => t.Name.Equals(resolver.Service) || t.FullName.Equals(resolver.Service)); if (resolverInterface == null) { config.Resolver.GetLog().Error(typeof(ExceptionlessExtraConfigurationExtensions), String.Format("An error occurred while retrieving service type \"{0}\".", resolver.Service)); continue; } try { Type implementationType = Type.GetType(resolver.Type); if (!resolverInterface.IsAssignableFrom(implementationType)) { config.Resolver.GetLog().Error(typeof(ExceptionlessExtraConfigurationExtensions), String.Format("Type \"{0}\" does not implement \"{1}\".", resolver.Type, resolver.Service)); continue; } config.Resolver.Register(resolverInterface, implementationType); } catch (Exception ex) { config.Resolver.GetLog().Error(typeof(ExceptionlessExtraConfigurationExtensions), ex, String.Format("An error occurred while registering service \"{0}\" implementation \"{1}\".", resolver.Service, resolver.Type)); } } } }
/// <summary> /// Sets the configuration from .net configuration settings. /// </summary> /// <param name="config">The configuration object you want to apply the settings to.</param> /// <param name="settings">The configuration settings</param> public static void ReadFromConfiguration(this ExceptionlessConfiguration config, IConfiguration settings) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var section = settings.GetSection("Exceptionless"); if (Boolean.TryParse(section["Enabled"], out bool enabled) && !enabled) { config.Enabled = false; } string apiKey = section["ApiKey"]; if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE") { config.ApiKey = apiKey; } string serverUrl = section["ServerUrl"]; if (!String.IsNullOrEmpty(serverUrl)) { config.ServerUrl = serverUrl; } if (TimeSpan.TryParse(section["QueueMaxAge"], out var queueMaxAge)) { config.QueueMaxAge = queueMaxAge; } if (Int32.TryParse(section["QueueMaxAttempts"], out int queueMaxAttempts)) { config.QueueMaxAttempts = queueMaxAttempts; } string storagePath = section["StoragePath"]; if (!String.IsNullOrEmpty(storagePath)) { config.Resolver.Register(typeof(IObjectStorage), () => new FolderObjectStorage(config.Resolver, storagePath)); } string storageSerializer = section["StorageSerializer"]; if (!String.IsNullOrEmpty(storageSerializer)) { try { var serializerType = Type.GetType(storageSerializer); if (!typeof(IStorageSerializer).GetTypeInfo().IsAssignableFrom(serializerType)) { config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), $"The storage serializer {storageSerializer} does not implemented interface {typeof(IStorageSerializer)}."); } else { config.Resolver.Register(typeof(IStorageSerializer), serializerType); } } catch (Exception ex) { config.Resolver.GetLog().Error(typeof(ExceptionlessConfigurationExtensions), ex, $"The storage serializer {storageSerializer} type could not be resolved: ${ex.Message}"); } } if (Boolean.TryParse(section["EnableLogging"], out bool enableLogging) && enableLogging) { string logPath = section["LogPath"]; if (!String.IsNullOrEmpty(logPath)) { config.UseFileLogger(logPath); } else if (!String.IsNullOrEmpty(storagePath)) { config.UseFileLogger(System.IO.Path.Combine(storagePath, "exceptionless.log")); } } if (Boolean.TryParse(section["IncludePrivateInformation"], out bool includePrivateInformation) && !includePrivateInformation) { config.IncludePrivateInformation = false; } if (Boolean.TryParse(section["ProcessQueueOnCompletedRequest"], out bool processQueueOnCompletedRequest) && processQueueOnCompletedRequest) { config.ProcessQueueOnCompletedRequest = true; } foreach (var tag in section.GetSection("DefaultTags").GetChildren()) { config.DefaultTags.Add(tag.Value); } foreach (var data in section.GetSection("DefaultData").GetChildren()) { if (data.Value != null) { config.DefaultData[data.Key] = data.Value; } } foreach (var setting in section.GetSection("Settings").GetChildren()) { if (setting.Value != null) { config.Settings[setting.Key] = setting.Value; } } // TODO: Support Registrations }
/// <summary> /// Reads the Exceptionless configuration from the app.config or web.config file. /// </summary> /// <param name="config">The configuration object you want to apply the attribute settings to.</param> public static void ReadFromConfigSection(this ExceptionlessConfiguration config) { ExceptionlessSection section = null; try { section = ConfigurationManager.GetSection("exceptionless") as ExceptionlessSection; } catch (Exception ex) { config.Resolver.GetLog().Error(typeof(ExceptionlessExtraConfigurationExtensions), ex, String.Concat("An error occurred while retrieving the configuration section. Exception: ", ex.Message)); } if (section == null) { return; } config.Enabled = section.Enabled; // Only update if it is not null if (!String.IsNullOrEmpty(section.ApiKey)) { config.ApiKey = section.ApiKey; } // If an appsetting is present for ApiKey, then it will override the other api keys string apiKeyOverride = ConfigurationManager.AppSettings["Exceptionless:ApiKey"] ?? String.Empty; if (!String.IsNullOrEmpty(apiKeyOverride)) { config.ApiKey = apiKeyOverride; } if (!String.IsNullOrEmpty(section.ServerUrl)) { config.ServerUrl = section.ServerUrl; } if (section.EnableSSL.HasValue) { config.EnableSSL = section.EnableSSL.Value; } if (!String.IsNullOrEmpty(section.StoragePath)) { config.UseFolderStorage(section.StoragePath); } if (section.EnableLogging.HasValue && section.EnableLogging.Value) { if (!String.IsNullOrEmpty(section.LogPath)) { config.UseFileLogger(section.LogPath); } else if (!String.IsNullOrEmpty(section.StoragePath)) { config.UseFileLogger(Path.Combine(section.StoragePath, "exceptionless.log")); } else { config.UseIsolatedStorageLogger(); } } foreach (var tag in section.Tags.SplitAndTrim(',').Where(tag => !String.IsNullOrEmpty(tag))) { config.DefaultTags.Add(tag); } if (section.ExtendedData != null) { foreach (NameValueConfigurationElement setting in section.ExtendedData) { if (!String.IsNullOrEmpty(setting.Name)) { config.DefaultData[setting.Name] = setting.Value; } } } if (section.Settings != null) { foreach (NameValueConfigurationElement setting in section.Settings) { if (!String.IsNullOrEmpty(setting.Name)) { config.Settings[setting.Name] = setting.Value; } } } }