Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 137229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:04:24+00:00 2026-05-11T07:04:24+00:00

I’m trying to implement a log viewer in my application. The idea is simple,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:04:24+00:00Added an answer on May 11, 2026 at 7:04 am

    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?’.


    it’s not a multithreaded application, is a freaking small application that I don’t want to overload.

    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:

    class MyClass {   Action<string> log;   MyClass(Action<string> log)   {     this.log = log;   }   void Something()   {     //log a message     log('Hello world!');   } } class MyForm {   ListBox listBox = new ListBox();   MyClass myClass;   MyForm()   {     //create a delegate which logs strings     //by writing them to the ListBox     Action<string> log = delegate(string s) {       listBox.Items.Add(s);     };     //pass this logger to classes which need to use it     myClass = new MyClass(log);     //test it     myClass.Something();   } } 

    Here’s similar code using a non-anonymous delegate, with some extra functionality as requested in one of your comments:

    class MyForm {   ListBox listBox = new ListBox();   MyClass myClass;   MyForm()   {     //pass the Log action to classes which need to use it     myClass = new MyClass(Log);     //test it     myClass.Something();   }    ///logs strings by writing them to the ListBox   void Log(string s)   {     if (listBox.Items.Count == 300)       listBox.Items.RemoveAt(0);     listBox.Items.Add(s);   } } 

    I think I answer wrong the (6). I should be able to send messages even if the listbox has not been created yet… that’s why I suggested a List or Queue

    In this case, you do need storage other than only the ListBox. Your suggestion would work. An alternative suggestion is:

    • When you create your ListBox, populate it from the List (using the ListBox.Items.AddRange method)
    • In the Log function, if the ListBox doesn’t exist then write the message to the List, else if the ListBox does exist then write the message to the ListBox.

    Doing this might be (very slightly) simpler than defining, listening to, and firing an event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SQL Server 2000 does not check for an empty table.… May 11, 2026 at 10:36 pm
  • Editorial Team
    Editorial Team added an answer The permission you are lacking is "view any definition". Go… May 11, 2026 at 10:36 pm
  • Editorial Team
    Editorial Team added an answer I answered a similar question in the following SO page:… May 11, 2026 at 10:36 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.