How do I Write to a Log File and display in a textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stoogots2
    New Member
    • Sep 2007
    • 77

    How do I Write to a Log File and display in a textbox

    I have written a windows application that performs several functions (is multi threaded). I have a Textbox in the main window of the app which I want to display the contents of the logfile and multiple threads are writing to the logfile. This has been very difficult (and frustrating) as I have wasted a lot of time without any positive result. I recently downloaded Log4Net and that is now giving me problems with reading the log and displaying it in the textbox.

    All I want is to allow multiple threads to write to the log file and the main form's thread the ability to read the file and display it in a textbox. Has anyone done this before? I just can't believe how much time I have wasted and not had this working at all.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Let me guess... You are having problems with various threads trying to access controls that were made from different threads.

    You can either use 'method invoke' to invoke a method on a different thread or you can raise an event with the item to be logged.

    Code:
            void RecordMajorEvent(string ToRecord)
            {
                _recordthis = ToRecord; // global variable
                this.Invoke(new MethodInvoker(RecordMajorEvent));
            }
            private void RecordMajorEvent()
            {
                this.eventHistoryTree1.AddMajorNode(_recordthis);
            }
    My normal way to do this is that I have a single class for logging. That class is created in Program.cs and is public. Now any form or class has access to that log class. My logging class has an event of "AddEntry". The "children" forms raise that event with the message to be logged.

    TIP: Instead of a textbox, try displaying in a listbox. This gives you a little more control. For example, you can keep track of the number of items in a listbox. If the items > 500, you can delete items 0-100, to keep the listbox from growing to thousands of lines degrading performance. Also new items automatically start new lines. You can click on an item and it highlights automatically, making it easier to read and so on.

    Comment

    • stoogots2
      New Member
      • Sep 2007
      • 77

      #3
      You are exactly right. The problem accessing a log file with more than one thread has been killing me. Thank you for the suggestion, and I will try to implement something similar in my code (and let you know if I get it to work).

      Regards,

      N

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Link updated

        This is something I wrote to someone else asking about events and delegates. I used a little of my logging as an example. Killing two birds with one stone.

        Last edited by tlhintoq; Jun 5 '09, 10:21 PM. Reason: Link updated

        Comment

        • stoogots2
          New Member
          • Sep 2007
          • 77

          #5
          The link isn't working for me. I am actually writing code on this today, so any more pointers are appreciated. I've chosen the Event route, but I don't understand how that gets around the multiple thread reading and writing to a file/accessing controls problem.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            I've updated the link. Give it another try.

            It gets around it because you won't have multiple threads reading/writing to the same file.
            You have one class that does the logging/reading/writing. A logging class. Thus it is the only thread accessing the file.

            Your other classes (regardless of thread) each raise an event with a text argument.
            Your logging class "hears" the event get raised and writes the message in the text argument.

            Comment

            Working...