/// <summary> /// Returns an <c>XmlConfiguration</c> object containing the child nodes specified by the input parameters /// </summary> /// <remarks> /// <P>This method looks under the current node context for a node named <c>section</c>. If found, /// it then looks for any child elements that contain a node with an attribute called <c>name</c> that /// have a value as defined by the <c>name</c> parameter. If found, then the <c>section</c> node /// is returned. If either the <c>section</c> node or the subnode with the <c>name</c> attribute /// value are not found, <c>Null</c> is returned</P> /// <P><B>Note:</B> if multiple matches are possible in the XML, only the first match is returned.</P></remarks> /// <param name="section"><c>String</c> that specifies the XML node name to search</param> /// <param name="name"><c>String</c> that specifies the value of the name attribute to match</param> /// <returns><c>XmlConfiguration</c> object with the identified XML node as the root node, or <c>Null</c> /// if not found</returns> public XmlConfiguration GetConfig( String section, String name) { XmlConfiguration retConfig = null; String xpath = ".//" + section + "/*[@name=\"" + name + "\"]"; //System.Diagnostics.Debug.WriteLine("XmlConfiguration.GetConfig: xpath= " + xpath); XmlNode node = SelectSingleNode(xpath); if (node != null) { retConfig = new XmlConfiguration(); retConfig.LoadXml(node.OuterXml); //System.Diagnostics.Debug.WriteLine("XmlConfiguration.GetConfig: xml= " + node.OuterXml); } return retConfig; }
/// <summary> /// Creates the Handler objects from the corresponding section in Loggers.config. /// </summary> /// <param name="config"><c>XmlConfiguration</c> section of Loggers.config to pull Handler object settings.</param> /// <returns></returns> private void FillHandlers( XmlConfiguration config) { NameValueCollection handlerColl = null; LogHandler handler = null; string handlerClass = null; String attrName = null; String attrValue = null; // Loop handler nodes for (int i = 1; i <= config.SelectNodes("//handlers/handler").Count; i++) { handlerColl = config.GetAttributes("//handlers/handler[" + i + "]"); try { // Create handler handlerClass = handlerColl["class"]; handler = (LogHandler) Activator.CreateInstance(Type.GetType(handlerClass)); // Loop handler attributes and dynamically create the objects properties for (int j = 0; j < handlerColl.Count; j++) { attrName = handlerColl.GetKey(j); attrValue = handlerColl.Get(j); // set the corresponding attribute of the object SetObjectAttribute(handler, attrName, attrValue); } // Add handler to collection this.AddHandler(handler); } catch (Exception e) { string m = e.Message; } handler = null; handlerClass = null; handlerColl = null; } }
/// <summary> /// Creates a filter to apply the the Logger from the corresponding section in Loggers.config. /// </summary> /// <param name="name"><c>String</c> that specifies the name of the <c>Logger</c> to lookup.</param> /// <returns><c>ILogFilter</c> set for Logger from config file.</returns> private static ILogFilter FillFilter( XmlConfiguration config) { ILogFilter filter = null; NameValueCollection filterColl = null; string filterClass = null; config = config.GetConfig("logger", "filter"); // Create the Filter object try { filterColl = config.GetAttributes("filter"); filterClass = filterColl["class"]; filter = (ILogFilter) Activator.CreateInstance(Type.GetType(filterClass)); } catch (Exception e) { // System.MissingMethodException } return filter; }
/// <summary> /// Creates the Logger object from settings in the Loggers.config. /// </summary> /// <param name="name"><c>String</c> that specifies the name of the <c>Logger</c> to lookup.</param> /// <returns><c>Logger</c> name</returns> private static Logger FillLogger( string name) { Logger log = null; XmlConfiguration config = new XmlConfiguration(); try { // get the path to the loggers.config file from web.config string loggersConfigPath = ConfigurationSettings.AppSettings["rootPath"]; // Load XmlConfiguration to get settings for this logger config.Load(loggersConfigPath + ConfigurationSettings.AppSettings["loggersConfigPath"]); //config.Load(new XmlTextReader(loggersConfigPath + ConfigurationSettings.AppSettings["loggersConfigPath"])); //config.Load(new XmlTextReader("http://fpwssdev1/PandaDen/XSLT%20Templates/loggers.config")); log = new Logger(name); // If there is configuration information, proceed, else return a new empty Logger if (config != null) { // this code will read the queue process time from a "settings" section of the config file: // <settings> // <!-- the time interval at which the log queue--> // <setting name="queSettings" queCheckTime="10000" /> // </settings> //XmlConfiguration queSettings = config.GetConfig("settings", QUEUE_SETTINGS_CONFIG_NAME); //if (queSettings != null) { // try { // NameValueCollection queAttr = queSettings.GetAttributes("./*[1]"); // int period = int.Parse(queAttr[QUEUE_SETTINGS_CHECK_TIME_ATTR]); // log.queTimer.Change(period, period); // } catch {} //} // get the logger's config settings config = config.GetConfig("loggers", name); NameValueCollection loggerAttrs = config.GetAttributes("./*[1]"); // Loop log collection attributes to dynamically set the log object's properties for (int i = 0; i < loggerAttrs.Count; i++) { string attrName = loggerAttrs.GetKey(i); string attrValue = loggerAttrs.Get(i); // if the attribute is the queue check time attribute, get the value // and update the queTimer with the new value (in milliseconds) if (attrName == QUEUE_SETTINGS_CHECK_TIME_ATTR) { try { int period = int.Parse(attrValue); log.queTimer.Change(period, period); } catch {} } else { // set the corresponding attribute of the object SetObjectAttribute(log, attrName, attrValue); } } // Add Filter and all Handler objects log.Filter = FillFilter(config); log.FillHandlers(config); // Logger objects must have at least one handler if (log.handlers.Count == 0) { log = null; } } } catch (Exception e) { log = null; } return log; }
public ArrayList Init( XmlConfiguration config) { ArrayList objList = new ArrayList(); #region Removing code thats not used anymore /* MemberInfo[] attrList; string attrValue; Type aType; PropertyInfo aProperty; Object propertyValue = null; MethodInfo aMethod; */ #endregion string className = config.GetValue("//object"); string tableName = config.GetValue("//table"); XmlNodeList columns = config.SelectNodes("//field"); // Build string array string[] fields = new string[columns.Count]; int i = 0; foreach (XmlNode node in columns) { fields.SetValue(node.Attributes["name"].Value, i); i++; } // Get data IGenericDao dao = DaoFactory.GetGenericDao(); DataSet ds = dao.GetData(tableName, fields); DataTable dt = ds.Tables[tableName]; // Loop through the data table to build the object list for (i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; Object anObj = Activator.CreateInstance(Type.GetType(className)); NameValueCollection attributes = new NameValueCollection(); foreach (XmlNode node in columns) { attributes.Add(node.Attributes["object_attr"].Value, dr[node.Attributes["name"].Value].ToString()); } ReflectionUtilities.Deserialize(anObj, attributes); #region Duplicate code from ReflectionUtilities /* aType = anObj.GetType(); // Loop through the fields in the config file to set properties for the object foreach (XmlNode node in columns) { attrList = aType.FindMembers(MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public, this.FindMembersCanWriteDelegate, //Type.FilterNameIgnoreCase, node.Attributes["object_attr"].Value); attrValue = dr[node.Attributes["name"].Value].ToString(); propertyValue = null; // TODO: Add error tracking code here if (attrList.Length == 1) { aProperty = (PropertyInfo) attrList[0]; if (aProperty.PropertyType.IsEnum) { // Use enumeration values defined in dbMapping if available. NameValueCollection enumConfig = (NameValueCollection) ConfigurationSettings.GetConfig("dbMappings/" + aProperty.PropertyType.Name); if (enumConfig != null && enumConfig[attrValue] != null) { attrValue = enumConfig[attrValue]; } aMethod = aProperty.PropertyType.BaseType.GetMethod( "Parse", new Type[3]{ Type.GetType("System.Type"), "".GetType(), true.GetType() }); if ((aMethod != null) && (aMethod.IsStatic)) { propertyValue = aMethod.Invoke( null, new object[3]{ aProperty.PropertyType, attrValue, true }); } } else if (Type.GetTypeCode(aProperty.PropertyType) == Type.GetTypeCode("".GetType())) { propertyValue = attrValue; } else { aMethod = aProperty.PropertyType.GetMethod("Parse", new Type[1]{"".GetType()}); if ((aMethod != null) && (aMethod.IsStatic)) { try { propertyValue = aMethod.Invoke( null, new object[1]{attrValue}); } catch (Exception e) { // TODO: Logger name should not be hardcoded here! LogManager.GetCurrentClassLogger().Error( errorMessage => errorMessage("DbSingletonBase.Init: PropertyType = '{0}', attrValue = '{1}'", aProperty.PropertyType, attrValue), e); } } } try { if (propertyValue != null) { aMethod = aProperty.GetSetMethod(); aMethod.Invoke(anObj, new[]{propertyValue}); } } catch (MissingMethodException e) { // TODO: Handle MissingMethodException LogManager.GetCurrentClassLogger().Error( errorMessage => errorMessage("Could not get the property value from field type on the object. Property Name: {0}, Property Type: {1} ", aProperty.PropertyType, attrValue), e); } } }*/ #endregion objList.Add(anObj); } dt.Dispose(); ds.Dispose(); return objList; }