0

I've an windows form application with a datagridview and each row is associated with it's own thread. Each thread calls different methods of an object, then logs entry, exit and other important events.

I want to display the logs corresponding to the datagridview row whenever the user clicks it. So that the selected row's messages will be displayed as running status logs.

I've thought about using Action delegate and a multiline textbox, but unsure how to filter log messages based on the particular datagridview row.

Please suggest me a method to implement this. Any help would be appreciated

Edit: The logs are to be displayed on UI in a textbox or something, not in the files which I do already. And the other thing is I want the logs to be updated continuously.

Edit2: I already have the logs appended to a string member of the row/thread and when the row is clicked, it will display the log messages saved so far. But in the thread, the string gets continuously updated with new messages. So I am looking for a way to display the running log messages which are updated in the backgroud thread.

Questions
  • 195
  • 1
  • 14

2 Answers2

0

If many threads use the same object it's maybe better to lock it so that way only one threat will change it at the time.

static object obj = new object();
            static void WriteInGrid(string message)
            { 
                lock(obj)
                {
                     Yourdelegate (message)
                }
            }
Bashkim Meka
  • 120
  • 6
  • I am looking for a way to display the log in UI using a textbox or something which gets updated continuously, only thing I am not able to figure out is how to display them based on the datagridview row(also thread) is selected. I already write them to the log files. – Questions Feb 02 '15 at 06:35
  • Maybe you should use lock to lock the object so that way threads will wait for each other to change the control property. – Bashkim Meka Feb 02 '15 at 06:49
0

To handle logging, the best approach is to reuse existing library such as log4net. These handle well multithreading, security and all other pitfall you wouldn't think for now.

Log4net works with appenders which are ways to append log records to one or many destinations (files, database, or whatever you want).

In your case you want to log to a UI component, which I think is not available out of the box. But you can write it yourself ! I think that in order to achieve this you just have to create a new class that will handle the UI logging logic and edit the log4net configuration accordingly.

This thread may be a good start in doing so ! It doesn't cover the logging in a grid but does in a textbox. Basically it's UI logging ! You will have to enhance it !

Community
  • 1
  • 1
John-Philip
  • 3,392
  • 2
  • 23
  • 52