Write log file using Thread

V

verenadoll

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
 
G

Ginny Caughey MVP

Verena,

What makes you think you will get better performance with many threads
writing into a file compared with only one? With multiple write access you
have to worry about region locks (and I can't remember if the Compact
Framework supports that yet - the OS only added region locks with CE 5), and
switching between threads also involves overhead.

There are often good reasons for multiple threads or processes to write to
the same multiuser database, and perceived performance is one of them.
Actual total performance generally isn't.
 
G

Ginny Caughey MVP

Verena,

What makes you think you will get better performance with many threads
writing into a file compared with only one? With multiple write access you
have to worry about region locks (and I can't remember if the Compact
Framework supports that yet - the OS only added region locks with CE 5), and
switching between threads also involves overhead.

There are often good reasons for multiple threads or processes to write to
the same multiuser database, and perceived performance is one of them.
Actual total performance generally isn't.
 
V

verenadoll

Hello Ginny,

thanks for your answer.

Actually my programm execute a function and at the end of the function
i write the duration time to a log file. After that next function was
executed and so on. every time the log file was written, my programm
has to "wait". So i thought using one thread and a queue would get
better performace, because writing the logfile, can be done in
backgroud.

i don´t understand, why this wouldn´t get better performance.

Regards, Verena Doll
 
V

verenadoll

Hello Ginny,

thanks for your answer.

Actually my programm execute a function and at the end of the function
i write the duration time to a log file. After that next function was
executed and so on. every time the log file was written, my programm
has to "wait". So i thought using one thread and a queue would get
better performace, because writing the logfile, can be done in
backgroud.

i don´t understand, why this wouldn´t get better performance.

Regards, Verena Doll
 
G

Ginny Caughey MVP

Try it and see. You said in your initial post that you intended to have
multiple threads attempting to insert data into a text file. Did I
misunderstand? Is there only one writer thread?
 
G

Ginny Caughey MVP

Try it and see. You said in your initial post that you intended to have
multiple threads attempting to insert data into a text file. Did I
misunderstand? Is there only one writer thread?
 
V

verenadoll

Yes there is only one writer thread. This threads looks at the queue
and writes the text to the file.
 
V

verenadoll

Yes there is only one writer thread. This threads looks at the queue
and writes the text to the file.
 
B

Batvanio

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


There is a very good logging framework (called log4net), it is open
source, and I woud suggest that you take a look at it, before starting
implementing your own.

http://logging.apache.org/log4net/index.html
 
B

Batvanio

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


There is a very good logging framework (called log4net), it is open
source, and I woud suggest that you take a look at it, before starting
implementing your own.

http://logging.apache.org/log4net/index.html
 
P

Paul G. Tobey [eMVP]

It's not clear from the docs of that project. Is Log() actually doing the
log write? Or is another thread getting the request and doing the log? If
it's not threaded in the way the OP wanted, yes, it's a great logging
environment, but it doesn't address the need here.

Paul T.

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


There is a very good logging framework (called log4net), it is open
source, and I woud suggest that you take a look at it, before starting
implementing your own.

http://logging.apache.org/log4net/index.html
 
P

Paul G. Tobey [eMVP]

It's not clear from the docs of that project. Is Log() actually doing the
log write? Or is another thread getting the request and doing the log? If
it's not threaded in the way the OP wanted, yes, it's a great logging
environment, but it doesn't address the need here.

Paul T.

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


There is a very good logging framework (called log4net), it is open
source, and I woud suggest that you take a look at it, before starting
implementing your own.

http://logging.apache.org/log4net/index.html
 
S

Simon Hart [MVP]

It sounds like you are talking about having an asynchronous process for
logging stuff so that you can return control to the user instead of blocking
the UI thread. This might improve perceived performance. But no doubt all
you are doing is writing 1 line or so at a time. The overhead of creating a
thread probably would not be worth it. As this creation is expensive not to
mention you will have to sync the file which causes blocking - and always
use the threadpool.

On a side note to good app design:
If you are logging exceptions, have you considered creating an exception
interceptor that gets executed when a given exception occurs. There are many
frameworks on the desktop that handle this but only one I know about on the
CF and that is PostSharp. This is often refered to as cross cutting
concerns. You could then use log4net to log data. (there is support for CF)

--
Simon Hart
Visual Developer - Device Application Development MVP
http://www.simonrhart.com

Hello Ginny,

thanks for your answer.

Actually my programm execute a function and at the end of the function
i write the duration time to a log file. After that next function was
executed and so on. every time the log file was written, my programm
has to "wait". So i thought using one thread and a queue would get
better performace, because writing the logfile, can be done in
backgroud.

i don´t understand, why this wouldn´t get better performance.

Regards, Verena Doll
 
S

Simon Hart [MVP]

It sounds like you are talking about having an asynchronous process for
logging stuff so that you can return control to the user instead of blocking
the UI thread. This might improve perceived performance. But no doubt all
you are doing is writing 1 line or so at a time. The overhead of creating a
thread probably would not be worth it. As this creation is expensive not to
mention you will have to sync the file which causes blocking - and always
use the threadpool.

On a side note to good app design:
If you are logging exceptions, have you considered creating an exception
interceptor that gets executed when a given exception occurs. There are many
frameworks on the desktop that handle this but only one I know about on the
CF and that is PostSharp. This is often refered to as cross cutting
concerns. You could then use log4net to log data. (there is support for CF)

--
Simon Hart
Visual Developer - Device Application Development MVP
http://www.simonrhart.com

Hello Ginny,

thanks for your answer.

Actually my programm execute a function and at the end of the function
i write the duration time to a log file. After that next function was
executed and so on. every time the log file was written, my programm
has to "wait". So i thought using one thread and a queue would get
better performace, because writing the logfile, can be done in
backgroud.

i don´t understand, why this wouldn´t get better performance.

Regards, Verena Doll
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top