Config: Improve Logging, Use Either AppConfig XOR ComputerConfig

This commit is contained in:
Jonathan Jenne 2020-12-14 15:28:57 +01:00
parent 16193b08cf
commit 9b47cb9ffb

View File

@ -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)
}
''' <summary>
''' 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`
''' </summary>
Private _WriteAllValuesToUserConfig As Boolean = False
''' <summary>
@ -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!")