Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Write log from multiple instances of an application.

Write log from multiple instances of an application.

Scheduled Pinned Locked Moved C#
helpcsharpalgorithmsquestionlearning
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    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.

    Richard DeemingR D L J 5 Replies Last reply
    0
    • N NJdotnetdev

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      N 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        N Offline
        N Offline
        NJdotnetdev
        wrote on last edited by
        #3

        Thanks a lot Richard for the advise. Will look in to it. :) :)

        L 1 Reply Last reply
        0
        • N NJdotnetdev

          Thanks a lot Richard for the advise. Will look in to it. :) :)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Depending on your requirements it may be easier for each application to provide a file or filename for the library to use as its log file.

          1 Reply Last reply
          0
          • N NJdotnetdev

            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.

            D Offline
            D Offline
            Daniel Pfeffer
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              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

              N Offline
              N Offline
              NJdotnetdev
              wrote on last edited by
              #6

              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[^]

              Richard DeemingR 1 Reply Last reply
              0
              • N NJdotnetdev

                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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                If you want simple, don't log to a "file" in this case; log to a database. Any decent logger will support this via configuration options.

                1 Reply Last reply
                0
                • N NJdotnetdev

                  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.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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)

                  1 Reply Last reply
                  0
                  • N NJdotnetdev

                    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[^]

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    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

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    1 Reply Last reply
                    0
                    • N NJdotnetdev

                      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.

                      J Offline
                      J Offline
                      John Torjo
                      wrote on last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups