I have a WPF app with an event log. I have an EventLog class I populate with saved events from an XML file when the app starts
namespace MyApp.Agent.EventLogging
{
public enum EventType
{
Infomation,
Error
}
public class EventLog
{
public String Image { get; set; }
public DateTime EventDate { get; set; }
public String EventText { get; set; }
}
}
public List<EventLog> GetSavedEvents()
{
string file = XmlUtilities.GetXmlLocation() + "\\Events.xml";
List<EventLog> elog = new List<EventLog>();
try
{
if (File.Exists(file))
{
Serialize<List<EventLog>> ser = new Serialize<List<EventLog>>();
elog = ser.DeserializeDocToObj(file);
}
}
catch
{
throw new InvalidEventLogException(ConfigurationManager.AppSettings.Get("EventLogFileInvalid"));
}
return elog;
}
I then convert this into an observable collection that a listview is bound to
List<EventLog> _evtLog = new List<EventLog>();
ObservableCollection<EventLog> _eventLog = new ObservableCollection<EventLog>();
_evtLog = logger.GetSavedEvents();
_evtLog.ForEach(x => _eventLog.Add(x));
I read that this is how it has to been done (although it seems a long winded way)
As the app is running new events are added to the observable collection. When the app closes I reverse this process to save the events.
While this works fine if there are only a few saved events as the list is getting bigger so the time it takes to do this is getting unrealistic (240K events is taking 7 secs). Ok first question you may ask is why would I want that many events anyway, truth is I don’t but it does highlight that I am not doing this the best way?
So my questions are:
Do I really need to populate the observable collection? Can I not make _evtLog Observable without populating one from the other?
Can I restrict these lists to X events based on the date of the event?
You could easily change
GetSavedEventsto serialize and deserialize anObservableCollection<EventLog>instead of aList<EventLog>.Having said that, the code to convert the list to an observable collection looks strange. Why aren’t you just using the appropriate constructor?
The problem with using
Addis that for each item being added you raise an event.