Rajesh a écrit :
Hi Mathieu,
Can you give me littel more insight that how do I use it. I will again try
to explain what I am aiming to achive -
I have three programs
1. FileWatcherService1
2. ProcessWatcherService1
3. MyUI
The two services will always run simultaneously and MyUI may run at any time.
And I have a DLL "LogManager" which writes to a log file "C:\abc.log". The
LogManager class is created using the singleton pattern.
Now I want to write my debug, error and information messages through the DLL
to C:\abc.log file. As stated by you, I am able to write to abc.log from
multiple threads from the same process. But I am not able to write from
multiple services / programs running on my machine.
I am not sure how to implement Threading.Mutex. Someone have suggested me
using the Shared Data segements in the DLL.
Can you give some reference about the implementation of Threading.Mutex.
Regards
Rajesh Thareja
"Mathieu Cartoixa" wrote:
>Tasos Vogiatzoglou a écrit :
>>Rajesh wrote:
We are tyring to build a DLL which will write the log data to a text file.
Multiple executables should use this dll to write data to same text file. We
are using a synchronized method (using TextWriter.Synchronized) for writing
data to the text file. The class in the DLL (LogManagement) is implemented
using the Singleton pattern.
My problem is that when I try to write data from more that one executables
(and services) it gives me error and does not allow write to the file.
Hi,
TextWriter.Synchronized() creates an wrapper that can only guarantee
that two threads in the same process will not conflict while writing
your data to the text file. It cannot guarantee that two threads in
different processes will not conflict either.
To prevent conflicts across processes, you will have to implement the
synchronization yourself, using the System.Threading.Mutex class for
instance.
Also, make sure that the method you use to create/open the text file
does not lock it to other processes...
Mathieu
Hi,
A good starting point in learning how to use mutexes could be this page
:
http://msdn.microsoft.com/library/en...asp?frame=true
I am sorry I do not have much time to write proper, compiling, fully
tested code, but something like this would do :
using System;
using System.IO;
using System.Threading;
public class LogManager
{
private static string _LogFileName=@"C:\abc.log";
// Better use a GUID here, so that the mutex name will not conflict
with another...
private Mutex _Mutex=new Mutex(false,
"66db34d1-1528-4083-b0c8-825d00f1aa7c");
/* Insert constructor, singleton (...) code here... */
public void Log(string logMessage)
{
_Mutex.WaitOne();
// Guarantee : only one thread of one single process at a time
in this section
using (StreamWriter sw=File.Append
Text(_LogFileName))
{
sw.WriteLine(logMessage);
sw.Close();
}
_Mutex.ReleaseHandle();
}
}
Note that the threads calling the method Log() will wait until they can
get the mutex, which may not be suitable in every situation...
Also, as suggested by Ignacio, consider using the event log...
Mathieu