How to safely write to a log file from two instances of the same application?

I have an application that can run only one instance at a time, however, if the 2nd instance is started, it must be registered in the general log file, which the first can also use.

I have a check on how many instances are running, and I planned to just register it in the event log first, but the application can work in a user or system context, and exceptions are thrown when trying to request the source of the event log so that the idea is canceled, since the security logs are not available for the user.

So, I wanted to know that the safest way to have 2 separate instances of the same application for writing to the log file is that it will provide them with the ability to write.

I would prefer not to use the existing additional structure if it can be avoided

Any help appreciated.

+3
source share
5 answers

A Mutex can be used to interprocess synchronize a shared resource, such as a log file. Here is a sample .

+3
source

You can always write to the system event log. No blocking or anything else is required, and the event viewer is more reliable than some for this.

SO. msdn, , .

.

+1

, ...

Windows, , . - .

0

syslog ? . , , . . , . ( - , )

0

- . , . .

0

Source: https://habr.com/ru/post/1715540/


All Articles