I have 2 independent DLLs that I’ll include as part of a solution. Each one can be used without the other and I’m now building #3.
The challenge I have is the each has a global configuration and each has a workstation(PC) configuration file.
Now the Station configuration is best left as its own file but the global configuration file well that’s my new issue.
In each DLL(Module) I setup a logging feature. The setting for this is in the global Config file. In addition each DLL has its own additional settings.
First DLL config
<GlobalConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LogFilesUNC>Logs</LogFilesUNC>
<AllowDBMaintenance></AllowDBMaintenance>
<DBKeepDays></DBKeepDays>
<ReportsUNC></ReportsUNC>
</GlobalConfigData>
Second DLL Config File:
<GlobalConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LogFilesUNC>Logs</LogFilesUNC>
<DBServerURI></DBServerURI>
<DBServerPort></DBServerPort>
</GlobalConfigData>
As you can see the LogFilesUNC is in both files. What I want is to read and process a single config file such as:
<GlobalConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LogFilesUNC>Logs</LogFilesUNC>
</GlobalConfigData>
<DLL1ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllowDBMaintenance></AllowDBMaintenance>
<DBKeepDays></DBKeepDays>
<ReportsUNC></ReportsUNC>
</DLL1ConfigData>
<DLL2ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DBServerURI></DBServerURI>
<DBServerPort></DBServerPort>
</DLL2ConfigData>
I want to keep thing such that DLL1 does not need to know about DLL2 in my code.
Here is how I read the files now:
//First define the file structure:
using System;
using System.Xml.Serialization;
namespace ReportMgr2022.Models
{
[Serializable(), XmlRoot()]
public class GlobalConfigData
{
public string LogFileUNC { get; set; } = @"Logs";
public bool AllowDBMaintenance { get; set; } = false;
public int DBKeepDays { get; set; } = 90;
}
}
And the code that processes the file:
namespace ReportMgr2022
{
internal class ConfigOptionsv2
{
private readonly string _CfgFQN;
public string Message { get; private set; }
public ConfigOptionsv2(string configFileFQN)
{
_CfgFQN = configFileFQN;
}
public bool ValidateConfigFile()
{
GlobalConfigData oOptions = null;
Message = string.Empty;
try
{
oOptions = LoadFromXmlFormat();
}
catch (Exception) { } //Error is ignorable
if (oOptions == null)
{
oOptions = new GlobalConfigData();
SaveAsXmlFormat(oOptions);
}
return string.IsNullOrWhiteSpace(Message);
}
public bool SaveAsXmlFormat(GlobalConfigData oOptions)
{
XmlSerializer xmlformat = new XmlSerializer(typeof(GlobalConfigData));
try
{
using (Stream fstream = new FileStream(_CfgFQN, FileMode.Create, FileAccess.Write, FileShare.None))
{
xmlformat.Serialize(fstream, oOptions);
}
}
catch (Exception ex)
{
Message = ParseException(ex);
return false;
}
return true;
}
public GlobalConfigData LoadFromXmlFormat()
{
XmlSerializer xmlformat = new XmlSerializer(typeof(GlobalConfigData));
GlobalConfigData oOptionsFromDisk = null;
try
{
using (Stream fstream = File.OpenRead(_CfgFQN))
{
oOptionsFromDisk = (GlobalConfigData) xmlformat.Deserialize(fstream);
}
}
catch (Exception ex)
{
throw new GlobalConfigException("ConfigOptionsv2.LoadFromXmlFormat", ParseException(ex), ex);
}
return oOptionsFromDisk;
}
}
[Serializable]
internal class GlobalConfigException : Exception
{
public GlobalConfigException() { }
public GlobalConfigException(string message) : base(message) { }
public GlobalConfigException(string source, string message, Exception inner) : base(message, inner) { Source = source; }
public GlobalConfigException(string message, Exception innerException) : base(message, innerException) { }
protected GlobalConfigException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
I know the load and save will need change but the issue I have seems to be when DLL1 updates the DLL2 data is wiped out. Based on the code that makes sense. Is ther a way to do this?
My thought is that I would need to read in the entire file any time I save and then modify the individual elements for the DLL and then write the entire file back out.
Or should I create a “Global config” for just the log file and keep everything else in its own file?