Write log from multiple instances of an application.
-
My C# dll is loaded by an application. I wanted to add logging in the dll. The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time. I found following approaches while searching, but not 100% sure if they would help: 1. Use FileShare Enumeration: But, this might cause problem if the first process hangs and blocks the resource. 2. Use Mutex: It was recommended to not use as it doesn't help when there are multiple processes accessing the same file. It is helpful when multiple threads are accessing the same file. Since, I can be wrong in my assumption so wanted to ask experts for some advise. Thanks.
-
My C# dll is loaded by an application. I wanted to add logging in the dll. The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time. I found following approaches while searching, but not 100% sure if they would help: 1. Use FileShare Enumeration: But, this might cause problem if the first process hangs and blocks the resource. 2. Use Mutex: It was recommended to not use as it doesn't help when there are multiple processes accessing the same file. It is helpful when multiple threads are accessing the same file. Since, I can be wrong in my assumption so wanted to ask experts for some advise. Thanks.
NJdotnetdev wrote:
But, this might cause problem if the first process hangs and blocks the resource.
Any scheme you can come up with will struggle if a process hangs whilst holding the lock on the file. You might want to consider using the Semantic Logging library[^] instead of your custom logging code. That way, you won't need to worry about synchronising access to the log file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
NJdotnetdev wrote:
But, this might cause problem if the first process hangs and blocks the resource.
Any scheme you can come up with will struggle if a process hangs whilst holding the lock on the file. You might want to consider using the Semantic Logging library[^] instead of your custom logging code. That way, you won't need to worry about synchronising access to the log file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks a lot Richard for the advise. Will look in to it. :) :)
-
Thanks a lot Richard for the advise. Will look in to it. :) :)
-
My C# dll is loaded by an application. I wanted to add logging in the dll. The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time. I found following approaches while searching, but not 100% sure if they would help: 1. Use FileShare Enumeration: But, this might cause problem if the first process hangs and blocks the resource. 2. Use Mutex: It was recommended to not use as it doesn't help when there are multiple processes accessing the same file. It is helpful when multiple threads are accessing the same file. Since, I can be wrong in my assumption so wanted to ask experts for some advise. Thanks.
The Microsoft Enterprise Library Enterprise Library 6 – April 2013[^] Has a block which provides a very flexible logging mechanism.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
-
NJdotnetdev wrote:
But, this might cause problem if the first process hangs and blocks the resource.
Any scheme you can come up with will struggle if a process hangs whilst holding the lock on the file. You might want to consider using the Semantic Logging library[^] instead of your custom logging code. That way, you won't need to worry about synchronising access to the log file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Quick question: Will there be a problem if multiple instances of the same application try to write in the same flat file at the same time? I found following text on MSDN: You should not configure more than one Flat File sink or Rolling Flat File sink instance to write to the same physical file. However, you can enable the same listener (and sink) on multiple event sources and funnel the events from those multiple event sources to the same file. Link: Logging events to a disk file[^]
-
My C# dll is loaded by an application. I wanted to add logging in the dll. The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time. I found following approaches while searching, but not 100% sure if they would help: 1. Use FileShare Enumeration: But, this might cause problem if the first process hangs and blocks the resource. 2. Use Mutex: It was recommended to not use as it doesn't help when there are multiple processes accessing the same file. It is helpful when multiple threads are accessing the same file. Since, I can be wrong in my assumption so wanted to ask experts for some advise. Thanks.
-
My C# dll is loaded by an application. I wanted to add logging in the dll. The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time. I found following approaches while searching, but not 100% sure if they would help: 1. Use FileShare Enumeration: But, this might cause problem if the first process hangs and blocks the resource. 2. Use Mutex: It was recommended to not use as it doesn't help when there are multiple processes accessing the same file. It is helpful when multiple threads are accessing the same file. Since, I can be wrong in my assumption so wanted to ask experts for some advise. Thanks.
NJdotnetdev wrote:
The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time.
You can't. I can create a new AppDomain and load your dll again. As long as the logger does what it is intended to do (log a message and uniquely identify the origin) things should work, even if loaded more than once, or, multiple versions of the same library :) Imagine writing a new product. Some dev finds out how to create a mini-dump, and includes an option to send the mini-dump along with the logging. Your new product uses YourCompanyLogging2.0, the improved logger; whereas the client has a different product on his/her machine which depends on YourCompanyLogging1.0. It knows nothing of the extra filepath it is supposed to send, and would throw and ArgumentNullException if the new routine is launched. There is a whole host of dragons ahead, and the subject you are diving into is called "DLL HEL". There is no neat solution there.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Quick question: Will there be a problem if multiple instances of the same application try to write in the same flat file at the same time? I found following text on MSDN: You should not configure more than one Flat File sink or Rolling Flat File sink instance to write to the same physical file. However, you can enable the same listener (and sink) on multiple event sources and funnel the events from those multiple event sources to the same file. Link: Logging events to a disk file[^]
Ah, OK, I hadn't noticed that. I was obviously thinking of using an out-of-process log. Using the trace event service for the out-of-process scenario[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
My C# dll is loaded by an application. I wanted to add logging in the dll. The only question in mind is how I can make sure that only one instance of the loaded dll is writing to the log file at a given time. I found following approaches while searching, but not 100% sure if they would help: 1. Use FileShare Enumeration: But, this might cause problem if the first process hangs and blocks the resource. 2. Use Mutex: It was recommended to not use as it doesn't help when there are multiple processes accessing the same file. It is helpful when multiple threads are accessing the same file. Since, I can be wrong in my assumption so wanted to ask experts for some advise. Thanks.
The question here is why do you want every one of your DLL instances to write in the same log? Why not have each instance of your DLL write to a different log? And besides, this probably makes more sense - since each app using your DLL will have its own workflow. Best, John
-- Log Wizard - a Log Viewer that is easy and fun to use!