From 9b47cb9ffb15d3d114526f6581eba6655310212b Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 14 Dec 2020 15:28:57 +0100 Subject: [PATCH] Config: Improve Logging, Use Either AppConfig XOR ComputerConfig --- Modules.Config/ConfigManager.vb | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Modules.Config/ConfigManager.vb b/Modules.Config/ConfigManager.vb index b44204a1..cfe7b639 100644 --- a/Modules.Config/ConfigManager.vb +++ b/Modules.Config/ConfigManager.vb @@ -17,8 +17,9 @@ Public Class ConfigManager(Of T) Private ReadOnly _UserConfigPath As String Private ReadOnly _ComputerDirectory As String Private ReadOnly _ComputerConfigPath As String - Private ReadOnly _AppConfigPath As String Private ReadOnly _AppConfigDirectory As String + Private ReadOnly _AppConfigPath As String + Private ReadOnly _TestMode As Boolean = False @@ -33,6 +34,19 @@ Public Class ConfigManager(Of T) GetType(GlobalSettingAttribute) } + ''' + ''' Signals that all properties will be written to (and read from) the UserConfig.xml + ''' + ''' If Value is `True`: + ''' - AppConfig.xml does NOT exist + ''' - ComputerConfig.xml does NOT exist + ''' - ConnectionStrings will be saved to or read from UserConfig.xml + ''' + ''' If Value is `False`: + ''' - No ConnectionStrings will be saved to or read from UserConfig.xml + ''' + ''' Can be overwritten by optional parameter `ForceUserConfig` + ''' Private _WriteAllValuesToUserConfig As Boolean = False ''' @@ -93,7 +107,7 @@ Public Class ConfigManager(Of T) End If If ApplicationStartupPath <> String.Empty Then - _Logger.Debug($"Appconfig is being used: {ApplicationStartupPath}") + _Logger.Debug($"AppConfig is being used: [{ApplicationStartupPath}]") _AppConfigPath = Path.Combine(ApplicationStartupPath, APP_CONFIG_NAME) End If @@ -137,9 +151,7 @@ Public Class ConfigManager(Of T) Dim oType As Type = GetType(T) Dim oExcludedAttributeTypes = IIf(IsNothing(ExcludedAttributeTypes), New List(Of Type), ExcludedAttributeTypes) Dim oProperties = oType.GetProperties(). - Where(Function(p) - Return p.CanRead And p.CanWrite - End Function). + Where(Function(p) p.CanRead And p.CanWrite). Where(Function(p) For Each oAttributeType As Type In oExcludedAttributeTypes If Attribute.IsDefined(p, oAttributeType) Then @@ -190,10 +202,13 @@ Public Class ConfigManager(Of T) Try Dim oAppConfig = ReadFromFile(_AppConfigPath) CopyValues(oAppConfig, Config) + + _Logger.Info("AppConfig exists and will be used. [{0}]", _AppConfigPath) Catch ex As Exception _Logger.Error(ex) _Logger.Warn("ApplicationConfig could not be loaded!") End Try + _WriteAllValuesToUserConfig = False Else _Logger.Debug("ApplicationConfig does not exist.") @@ -204,10 +219,14 @@ Public Class ConfigManager(Of T) End Function Private Function LoadComputerConfig(ByVal Config As T) As T - If File.Exists(_ComputerConfigPath) Then + If _WriteAllValuesToUserConfig = False Then + _Logger.Info("AppConfig exists. ComputerConfig will NOT be used") + ElseIf File.Exists(_ComputerConfigPath) Then Try Dim oComputerConfig = ReadFromFile(_ComputerConfigPath) CopyValues(oComputerConfig, Config) + + _Logger.Info("ComputerConfig exists and will be used. [{0}]", _ComputerConfigPath) Catch ex As Exception _Logger.Error(ex) _Logger.Warn("Computer config could not be loaded!") @@ -237,6 +256,8 @@ Public Class ConfigManager(Of T) CopyValues(oUserConfig, Config, _ExcludedAttributes) End If End If + + _Logger.Info("UserConfig exists and will be used. [{0}]", _UserConfigPath) Catch ex As Exception _Logger.Error(ex) _Logger.Warn("User config could not be loaded!")