|
|
|
start date: Tue, 17 Jul 2007 10:28:48 +0300,
posted on: microsoft.public.dotnet.framework.compactframework
back
| Thread Index |
|
1
Moshe Peleg
|
|
2
Simon Hart
|
|
3
Monkian
|
|
4
Ebbe Kristensen
|
|
5
Moshe Peleg
|
|
6
unknown
|
Logging
Hi,
I'm developing a multiple threads application.
I need to log several events in the different threads into the same text
file.
The add_2_log function is very simle:
using (StreamWriter sw = new StreamWriter(log_file_name,true))
{
sw.Write (DateTime.Now.Date.Day.ToString().PadLeft(2, '0') +"/" +
DateTime.Now.Date.Month.ToString().PadLeft(2, '0') + "/" +
DateTime.Now.Date.Year.ToString() + "," + DateTime.Now.TimeOfDay.ToString()
+ "," + message + "," + code1.ToString() + "," + code2.ToString());
}
Do I need to add a Mutex in order to prevent thread trying to add a line
while file is already open for current ?
Thanks,
MP
Date:Tue, 17 Jul 2007 10:28:48 +0300
Author:
|
RE: Logging
Yes a mutex would be required or a lock would work just as well.
--
Simon Hart
http://simonrhart.blogspot.com
"Moshe Peleg" wrote:
> Hi,
>
> I'm developing a multiple threads application.
> I need to log several events in the different threads into the same text
> file.
> The add_2_log function is very simle:
>
> using (StreamWriter sw = new StreamWriter(log_file_name,true))
> {
>
> sw.Write (DateTime.Now.Date.Day.ToString().PadLeft(2, '0') +"/" +
> DateTime.Now.Date.Month.ToString().PadLeft(2, '0') + "/" +
> DateTime.Now.Date.Year.ToString() + "," + DateTime.Now.TimeOfDay.ToString()
> + "," + message + "," + code1.ToString() + "," + code2.ToString());
>
> }
>
>
>
> Do I need to add a Mutex in order to prevent thread trying to add a line
> while file is already open for current ?
>
>
>
> Thanks,
>
>
>
> MP
>
>
>
Date:Tue, 17 Jul 2007 01:28:00 -0700
Author:
|
Re: Logging
Using lock(obj) in C# would be preferred as this compiles to
Monitor.Enter(obj)
try
{
//work
}
finally
{
Monitor.Exit(obj);
}
Monitor exposes locking mechanics supported by the CLR.
Using a Mutex in managed code simply provides a managed wrapper around a
kernal object and as such is the more heavyweight solution. Mutexes should
really only be used where inter-process synchronisation is required.
"Simon Hart" wrote in message
news:59FD1322-3C45-420D-815C-94AFF8E6C31D@microsoft.com...
> Yes a mutex would be required or a lock would work just as well.
> --
> Simon Hart
> http://simonrhart.blogspot.com
>
>
> "Moshe Peleg" wrote:
>
>> Hi,
>>
>> I'm developing a multiple threads application.
>> I need to log several events in the different threads into the same text
>> file.
>> The add_2_log function is very simle:
>>
>> using (StreamWriter sw = new StreamWriter(log_file_name,true))
>> {
>>
>> sw.Write (DateTime.Now.Date.Day.ToString().PadLeft(2, '0') +"/" +
>> DateTime.Now.Date.Month.ToString().PadLeft(2, '0') + "/" +
>> DateTime.Now.Date.Year.ToString() + "," +
>> DateTime.Now.TimeOfDay.ToString()
>> + "," + message + "," + code1.ToString() + "," + code2.ToString());
>>
>> }
>>
>>
>>
>> Do I need to add a Mutex in order to prevent thread trying to add a line
>> while file is already open for current ?
>>
>>
>>
>> Thanks,
>>
>>
>>
>> MP
>>
>>
>>
Date:Tue, 17 Jul 2007 10:09:50 +0100
Author:
|
Re: Logging
Moshe Peleg wrote:
> Do I need to add a Mutex in order to prevent thread trying to add a
> line while file is already open for current ?
Yes - but keep in mind that if one thread blocks another because of logging,
the flow of execution in your application has changed from what it would be
without logging.
Ebbe
Date:Tue, 17 Jul 2007 11:52:16 +0200
Author:
|
Re: Logging
Thank you very much.
Does the same answer goes for timers ?
Is it the same for the "ordinary" control timers ?
I understand they are on the same thread, but the do interrupt the main
program.
Therefore they also might cause the same problem.
"Ebbe Kristensen" wrote in message
news:%23eP5pgFyHHA.3400@TK2MSFTNGP03.phx.gbl...
> Moshe Peleg wrote:
>
>> Do I need to add a Mutex in order to prevent thread trying to add a
>> line while file is already open for current ?
>
> Yes - but keep in mind that if one thread blocks another because of
> logging, the flow of execution in your application has changed from what
> it would be without logging.
>
> Ebbe
>
Date:Wed, 18 Jul 2007 11:21:32 +0300
Author:
|
Re: Logging
Yeah, how about having the threads submit log messages to a queue, and
have another thread reading from the queue and writing the log
messages to a file?
Matt
On Jul 17, 10:52 am, "Ebbe Kristensen" wrote:
> Moshe Peleg wrote:
> > Do I need to add a Mutex in order to prevent thread trying to add a
> > line while file is already open for current ?
>
> Yes - but keep in mind that if one thread blocks another because of logging,
> the flow of execution in your application has changed from what it would be
> without logging.
>
> Ebbe
Date:Wed, 18 Jul 2007 09:45:35 -0700
Author:
|
|
|