I’m trying to implement a log viewer in my application. The idea is simple, a listbox that shows some messages sended from other classes, but I don’t know what is the best way to do this.
My initial thought was to make a Logger class (singletone) that contains a List or Queue, then I’ll add a method AddMessage(string s) or something like that. When this method is called it will add the message to the list or queue and it will fire a NewMessage event. This event is because I don’t think that is a good idea to check the list every some amount of time. The sequence of messages could be 3 consecutives, then 40 minutes without nothing, then a few more …
So, in my form class (or wherever I want to receive the messages) I will listen to this event to empty the list or queue (this is because I should be able to send messages even when the listbox (final receptor) has not been created). The idea of the list is to save messages when no one is listening the event.
Also, i’ve put a restriction of 300 messages… so the oldest ones will be deleted everytime i’m going to add new ones… something like this:
while(listbox.Items.Count > 300) { listbox.Items.RemoveAt(0); }
Do you think that this is the best approach?
Thanks for your time. Best regards.
Edit 1: I don’t want to use another framework.
Edit 2: STOP suggesting frameworks or another application. I want to learn the logic behind, and if what i was proposing is right or wrong!
Some of the questions to consider are:
Is your application multi-threaded? Is the thread[s] which write[s] the messages the same as the UI thread which reads the messages and populates the list box?
How many messages per second and being created?
How many times per second do you want to update the list box?
What’s the maximum number of messages over time? Do you display them all in the list box, or do you discard older ones?
Do you want log the messages into a file as well (e.g. for tech support purposes)?
Is the listbox (the UI element) the only place where messages are stored? Is the listbox always displayed, or can the user hide/destroy it?
How sophisticated are the end-users? Can you ask them to use other (better) tools to view your log file, or do you need to implement a (relatively unsophisticated) viewer into your own UI?
Sorry to answer your question with questions, but these are questions which need answering before you can say ‘is this the best approach?’.
If the application isn’t multi-threaded, then the ‘simplest thing that could possibly work’ would be to not bother with a list or queue; and instead, let your other classes append messages directly into the UI listbox element (perhaps via a delegate), for example:
Here’s similar code using a non-anonymous delegate, with some extra functionality as requested in one of your comments:
In this case, you do need storage other than only the ListBox. Your suggestion would work. An alternative suggestion is:
Doing this might be (very slightly) simpler than defining, listening to, and firing an event.