private void trySetPropertiesFromConfigFile() { string configFilePath = GetConfigFileFullPath(); if (File.Exists(configFilePath)) { try { LogFileFactoryConfig newLogFileFactoryConfig = LogFileFactoryConfig.LoadFromFile(configFilePath); bool resetIsInitializing = false; try { lock (SyncLock) { resetIsInitializing = !isInitializing; isInitializing = true; } newLogFileFactoryConfig.SetPropertiesOn(this); } finally { lock (SyncLock) { logFileFactoryConfig = newLogFileFactoryConfig; if (resetIsInitializing) { isInitializing = false; } } } TraceSources.For(TraceFileFactoryAssembly) .Info( "Found {0} file found at '{1}' - '{2}'.", nameof(LogFileFactoryConfig), configFilePath, logFileFactoryConfig); } catch (Exception exception) { TraceSources.For(TraceFileFactoryAssembly) .Error( exception, "Invalid {0} file found at '{1}' - '{2}'.", nameof(LogFileFactoryConfig), configFilePath, exception.Message); } } else { try { Directory.CreateDirectory(GetLogFolderPath()); LogFileFactoryConfig.CreateFrom(this, out XmlDocument xmlDocument); File.WriteAllText(configFilePath, xmlDocument.InnerXml); } catch (Exception exception) { TraceSources.For(TraceFileFactoryAssembly) .Error( exception, "Error writing {0} file to '{1}' - '{2}'.", nameof(LogFileFactoryConfig), configFilePath, exception.Message); } } }
/// <summary> /// Tries to load an Xml file at the <paramref name="filePath"/> and parse /// and set property values on a new <see cref="LogFileFactoryConfig"/> /// instance. The file can be formed with elements like this: /// <code> /// <LogFileFactoryConfig> /// <DefaultTraceSourceSelection>All</DefaultTraceSourceSelection> /// <-- ... --> /// </LogFileFactoryConfig> /// </code> /// And this method will also first try to parse any attributes defined on the root element /// with the property names --- which will take precedence over child elements. /// </summary> /// <param name="filePath">Required.</param> /// <returns>Not null.</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="XmlException"></exception> public static LogFileFactoryConfig LoadFromFile(string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(nameof(filePath)); } XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(filePath); } catch (FileNotFoundException) { throw; } catch (XmlException) { throw; } catch (Exception exception) { throw new XmlException( $"Unable to parse Xml document at '{filePath}'. --- '{exception.Message}'.", exception); } if (xmlDocument.DocumentElement == null) { throw new XmlException($"Unable to parse Xml document at '{filePath}'."); } LogFileFactoryConfig logFileFactoryConfig = new LogFileFactoryConfig(); foreach (PropertyDescriptor property in LogFileFactoryConfig.GetConfigProperties()) { if (!xmlDocument.DocumentElement.HasChildNodes) { if (property.CanResetValue(logFileFactoryConfig)) { property.ResetValue(logFileFactoryConfig); } continue; } bool reset = true; foreach (XmlNode node in xmlDocument.DocumentElement.GetElementsByTagName(property.Name)) { if (!TryParseAndSetProperty(logFileFactoryConfig, property, node.InnerText)) { continue; } reset = false; break; } if (reset && property.CanResetValue(logFileFactoryConfig)) { property.ResetValue(logFileFactoryConfig); } } return(logFileFactoryConfig);
static bool TryParseAndSetProperty( LogFileFactoryConfig config, PropertyDescriptor property, string xmlValue) { if ((property.PropertyType == typeof(LogFileFactorySelection)) || (property.PropertyType == typeof(LogFileFactorySelection?))) { if (!string.IsNullOrEmpty(xmlValue) && Enum.TryParse(xmlValue, out LogFileFactorySelection value)) { property.SetValue(config, value); return(true); } } else if ((property.PropertyType == typeof(SourceLevels)) || (property.PropertyType == typeof(SourceLevels?))) { if (!string.IsNullOrEmpty(xmlValue) && Enum.TryParse(xmlValue, out SourceLevels value)) { property.SetValue(config, value); return(true); } } else if ((property.PropertyType == typeof(bool)) || (property.PropertyType == typeof(bool?))) { if (!string.IsNullOrEmpty(xmlValue) && bool.TryParse(xmlValue, out bool value)) { property.SetValue(config, value); return(true); } } if (property.CanResetValue(config)) { property.ResetValue(config); } return(false); }