PC Review


Reply
Thread Tools Rate Thread

Wirte Logs using Threads

 
 
verenadoll@gmail.com
Guest
Posts: n/a
 
      19th May 2009
Hello NG,

in my application i log many events or functions (with duration time)
in a text file, so that i can reproduce if an exception was fired. It
works fine.

Now i want to use a thread (or many threads) for writing into the text
file to get better performance. But i have no experience using thread.
What will i have to use? ThreadPool or Queue or ThreadWrapperBase?

Maybe someone has an example for me?

Regards, Verena
 
Reply With Quote
 
 
 
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      19th May 2009
You won't get better performance by using a bunch of threads. And you'll
have to handle the case of two writers interacting with each other resulting
in bits and pieces of messages being interspersed. Especially if you are
unfamiliar with threading, use a single thread to do the writing and send
messages to it from any thread that wants to do logging. This will remove
the time taken to write the file from the main thread, which I presume is
what you mean by getting better performance. To communicate from other
threads to the logging thread, I would suggest a queue class that you write
yourself, based on Queue<string>, probably (to store the messages to be sent
to the file), and an event which is fired when there are messages in the
queue. Any thread in the application can then call:

// Log a string.
theQueue.Enqueue( "Here is some text to be written to the file" );

Then, you need a logging thread to remove items from the queue when they
appear and write them to the log file. You have to start this thread early
on:

// Create logging thread.
exitNow = false;
loggingThread = new Thread( new ThreadStart( this.ThreadProc ) );
loggingThread.Start();

The logging thread itself might look something like this:

// Open the file.

// Seek to the beginning and clear the data or seek to the end to append.

// Loop as long as the main thread doesn't tell us to exit.
while ( theQueue.Wait() && !exitNow )
{
// Get the next message from the queue.
string msg = theQueue.Dequeue();

// Write it to the file.
}

Obviously, I'm not going to write all the code for you, but I hope that you
get the idea.

Paul T.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello NG,
>
> in my application i log many events or functions (with duration time)
> in a text file, so that i can reproduce if an exception was fired. It
> works fine.
>
> Now i want to use a thread (or many threads) for writing into the text
> file to get better performance. But i have no experience using thread.
> What will i have to use? ThreadPool or Queue or ThreadWrapperBase?
>
> Maybe someone has an example for me?
>
> Regards, Verena



 
Reply With Quote
 
 
 
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      19th May 2009
You won't get better performance by using a bunch of threads. And you'll
have to handle the case of two writers interacting with each other resulting
in bits and pieces of messages being interspersed. Especially if you are
unfamiliar with threading, use a single thread to do the writing and send
messages to it from any thread that wants to do logging. This will remove
the time taken to write the file from the main thread, which I presume is
what you mean by getting better performance. To communicate from other
threads to the logging thread, I would suggest a queue class that you write
yourself, based on Queue<string>, probably (to store the messages to be sent
to the file), and an event which is fired when there are messages in the
queue. Any thread in the application can then call:

// Log a string.
theQueue.Enqueue( "Here is some text to be written to the file" );

Then, you need a logging thread to remove items from the queue when they
appear and write them to the log file. You have to start this thread early
on:

// Create logging thread.
exitNow = false;
loggingThread = new Thread( new ThreadStart( this.ThreadProc ) );
loggingThread.Start();

The logging thread itself might look something like this:

// Open the file.

// Seek to the beginning and clear the data or seek to the end to append.

// Loop as long as the main thread doesn't tell us to exit.
while ( theQueue.Wait() && !exitNow )
{
// Get the next message from the queue.
string msg = theQueue.Dequeue();

// Write it to the file.
}

Obviously, I'm not going to write all the code for you, but I hope that you
get the idea.

Paul T.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello NG,
>
> in my application i log many events or functions (with duration time)
> in a text file, so that i can reproduce if an exception was fired. It
> works fine.
>
> Now i want to use a thread (or many threads) for writing into the text
> file to get better performance. But i have no experience using thread.
> What will i have to use? ThreadPool or Queue or ThreadWrapperBase?
>
> Maybe someone has an example for me?
>
> Regards, Verena



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wirte Logs using Threads verenadoll@gmail.com Microsoft Dot NET Compact Framework 0 19th May 2009 10:50 AM
Using VBA to wirte script. =?Utf-8?B?Um9iZXJ0?= Microsoft Access 12 22nd Jun 2005 09:16 PM
help:I want to wirte a reminder programme,but how implement it in CF Yang_He Microsoft Dot NET Compact Framework 1 6th Feb 2005 01:19 PM
How do i wirte a letter of apology ? =?Utf-8?B?am1vbmU=?= Microsoft Word Document Management 3 3rd Dec 2004 08:48 PM
How to wirte a char to a specified location in a txt file? Microsoft VB .NET 1 29th Oct 2003 11:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:07 AM.