Sharing Data in DLLs

G

Guest

Hi,

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.

Any help is appreciated.

Thanks

Rajesh
http://www.slcltd.com
 
G

Guest

It give "File in use".

Basically I am getting multiple instance of the DLL and the singleton
pattern is not working.

Regards
Rajesh
 
M

Mathieu Cartoixa

Tasos Vogiatzoglou a écrit :
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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You should have the file open the less possible time, so you should
open/close it in each call.
Even more, it's possible that more than one process will try to write to it
at the same time. so you will still have a problem.

Possible solutions:
1- Use event log instead of a file.
2- Use a dedicated thread to write down the events, this thread will use a
sync queue to store the log entries that need to be written. it will fire
from time to time and try to write the entries to the file. The entries will
be generated by the UI thread (or any other worker thread)

Hope this help,
 
G

Guest

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
 
M

Mathieu Cartoixa

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

Hi,

A good starting point in learning how to use mutexes could be this page
:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmutex.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
 

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