One DLL writes to single file and used by multiple application

G

Guest

Hi All,

I am facing one problem. I have one DLL called LogManagement which has
ability to write to ExceptionLog and EventLog File. Both are separate file
locate in Logs Diectory. I have two windows service application and one
windows application which call the LogManagement DLL to write to log file.

I made the object of LogManagement as Singleton but when I run single
application, It runs perfect, but when I run

multiple application then It raises error. I can't debug because It's a
windows service but I know the

problem is EACH application is creating its own Object, I don't want to
create the object each time Because when I am initializing object I am also
checking that both the file exist or not ? If does not exist then create a
new file on same location. When file created one windows service starting
using the file to write event logs.
 
M

Marc Gravell

One option would be to:
a: guard all file access by a global mutex
b: only hold the file (and mutex) open when actively writing (or creating)

This should allow bboth processes independent access to the file... you
could also use remoting to ensure that all writing goes through a single
process, but that is probably more brittle.

Marc
 
I

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

Hi,

kumar_anil_gaya_India said:
Hi All,

I am facing one problem. I have one DLL called LogManagement which has
ability to write to ExceptionLog and EventLog File. Both are separate
file
locate in Logs Diectory. I have two windows service application and one
windows application which call the LogManagement DLL to write to log file.

I made the object of LogManagement as Singleton but when I run single
application, It runs perfect, but when I run

multiple application then It raises error.

Each app will have a copy of the dll so you will have three objetcs, one in
each program.
I can't debug because It's a
windows service

You can debug a win service, just attach the debugger to the process. (from
the debug menu)


There are several ways to solve this, the easiest is to create a new event
Log, one only used by your apps. doing this you do not have to take care of
concurrency problems.
 
G

Guest

Hi Machine,
My requirement is to write to single file, my client is saying that we need
a single to manage all processes. We have windows service and windows
application which has some out put to write to same txt file. Every
application is using the same file. Basically One DLL is handling the writing
process. Ever application is using singleton object which is defined in the
DLL. But When I get the instance it creates new object every time in every
application.
 
M

Marc Gravell

Yes; that will happen. .Net singletons are scoped by their AppDomain, so
each process will get their own, separate instances that are ignorant of
eachother. Hence the need for a mutex, IPC, remoting or similar.

Marc
 
B

Brian Gideon

You can debug a windows service by attaching the debugger via the Debug
| Processes menu item. I recommend setting up the application to run
in two different modes: 1) Windows service and 2) Interactive
application. It's easier to debug when the application is running in
an interactive environment. If you don't want to take the time to get
that setup then there is a simple trick you can use.

public static void Main()
{
if (System.Diagnostics.Debugger.IsAttached)
{
// Run it as an interactive application.
Service1 service = new Service1();
service.OnStart(null);
Thread.CurrentThread.Join();
}
else
{
// Run it as a service application.
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}

There are several variations on this theme including the use of
conditional compilation.

Brian
 
I

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

Hi,

I do not know if it was you or somebody else from your team, but if you look
for a thread named "sharing data in dlls" you will see a post there where I
gave a possible solution to this
 

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