/// <summary> /// Deserializes the Events from file and keeps them in RAM /// </summary> private static void Deserialize() { const string CATEGORY_NODE="Category"; const string EVENT_NODE="Event"; string strAction=""; DataSet ds=new DataSet(); DataTable dt; string strConfigFile=Logging.GetAssemblyDirectory()+"\\"+EVENTS_CODES_FILE; try { strAction="Reading config file."; ds.ReadXml(strConfigFile); #region Read Categories strAction="Attempting to read ["+CATEGORY_NODE+"] Table.number of tables="+ds.Tables.Count.ToString(); dt=ds.Tables[CATEGORY_NODE]; if(dt==null) { throw new Exception("Could not find ["+CATEGORY_NODE+"] elements."); } //We do not clear the dictionary beforehand. If it is not clean, the cause must be fixed. We do not expect to deserialize more than once. int intCounter=0; foreach(DataRow dr in dt.Rows) { intCounter++; strAction="Reading "+CATEGORY_NODE+" element #"+intCounter.ToString(); EventCategory Cat=new EventCategory(); Cat.Id=byte.Parse(dr["Id"].ToString()); Cat.Type=dr["Type"].ToString(); _EventCategories.Add(Cat); } #endregion #region Read Events strAction="Attempting to read ["+EVENT_NODE+"] Table."; dt=ds.Tables[EVENT_NODE]; if(dt==null) { throw new Exception("Could not find ["+EVENT_NODE+"] elements."); } //We do not clear the dictionary beforehand. If it is not clean, the cause must be fixed. We do not expect to deserialize more than once. intCounter=0; foreach(DataRow dr in dt.Rows) { intCounter++; strAction="Reading "+EVENT_NODE+" element #"+intCounter.ToString(); EventMetadata Evt=new EventMetadata(); Evt.Id=int.Parse(dr["Id"].ToString()); strAction+=", Id="+Evt.Id.ToString(); Evt.Category.Id=byte.Parse(dr["Category"].ToString()); Evt.Type=(System.Diagnostics.EventLogEntryType)int.Parse(dr["Type"].ToString()); Evt.Text=dr["Text"].ToString(); _dicEvents.Add(Evt.Id,Evt); } #endregion } catch(Exception e) { Logging.RelayException("Configuration File ["+strConfigFile+"]: "+strAction,e); } finally { if(ds!=null) { ds.Dispose(); } } }
/// <summary> /// Deserializes the Events from file and keeps them in RAM /// </summary> private static void Deserialize() { const string CATEGORY_NODE = "Category"; const string EVENT_NODE = "Event"; string strAction = ""; DataSet ds = new DataSet(); DataTable dt; string strConfigFile = Logging.GetAssemblyDirectory() + "\\" + EVENTS_CODES_FILE; try { strAction = "Reading config file."; ds.ReadXml(strConfigFile); #region Read Categories strAction = "Attempting to read [" + CATEGORY_NODE + "] Table.number of tables=" + ds.Tables.Count.ToString(); dt = ds.Tables[CATEGORY_NODE]; if (dt == null) { throw new Exception("Could not find [" + CATEGORY_NODE + "] elements."); } //We do not clear the dictionary beforehand. If it is not clean, the cause must be fixed. We do not expect to deserialize more than once. int intCounter = 0; foreach (DataRow dr in dt.Rows) { intCounter++; strAction = "Reading " + CATEGORY_NODE + " element #" + intCounter.ToString(); EventCategory Cat = new EventCategory(); Cat.Id = byte.Parse(dr["Id"].ToString()); Cat.Type = dr["Type"].ToString(); _EventCategories.Add(Cat); } #endregion #region Read Events strAction = "Attempting to read [" + EVENT_NODE + "] Table."; dt = ds.Tables[EVENT_NODE]; if (dt == null) { throw new Exception("Could not find [" + EVENT_NODE + "] elements."); } //We do not clear the dictionary beforehand. If it is not clean, the cause must be fixed. We do not expect to deserialize more than once. intCounter = 0; foreach (DataRow dr in dt.Rows) { intCounter++; strAction = "Reading " + EVENT_NODE + " element #" + intCounter.ToString(); EventMetadata Evt = new EventMetadata(); Evt.Id = int.Parse(dr["Id"].ToString()); strAction += ", Id=" + Evt.Id.ToString(); Evt.Category.Id = byte.Parse(dr["Category"].ToString()); Evt.Type = (System.Diagnostics.EventLogEntryType) int.Parse(dr["Type"].ToString()); Evt.Text = dr["Text"].ToString(); _dicEvents.Add(Evt.Id, Evt); } #endregion } catch (Exception e) { Logging.RelayException("Configuration File [" + strConfigFile + "]: " + strAction, e); } finally { if (ds != null) { ds.Dispose(); } } }