1
votes

I have written two different applications running as windows services. Each application represents one source to the windows logs. 1.) Question: I want the applications to write entries to the same log e.g. my_applications_log. My motivation is to monitor order of the actions taken by the applications. Problem is that when I try to write an entry with one source and then another entry with another source to the same log, only the entry with the latest source registered is written to the log, the other one not. I don't understand why this should not be possible.. I know one way is to split logs according to sources and use custom view to have them "joined". But I would like to have one specified log not two or more..

2.) question: In Windows Event Viewer I can see that logs can be organized into "directories" e.g.: Application and Services Logs->Microsoft->Windows->Audio->{CaptureMonitor log, Operational log} . I can not find any API allowing to create such a directory and the some logs within this directory. Is this possible somehow??

thanks in advance

1

1 Answers

1
votes

If you're trying to share a log file across processes, you need to protect that file with a lock of some kind, or have the applications retry if a write fails. It's not possible for two processes to be writing to the same file at the same time. Two processes can have the file open for writing, but if they both try to write at the same time, then you'll likely get an exception.

Probably the easiest way to do this is with a Mutex. Each application would create a named Mutex at startup, using the same name:

Mutex LogLock = new Mutex(false, "LogLock");

Then, when you want to write to the file:

LogLock.WaitOne();
try
{
    // write to the file
}
finally
{
    LogLock.ReleaseMutex();
}

If you want to write to the Windows event log, look into the System.Diagnostics.EventLog class. I don't have any experience with writing to the Windows event log, so I can't say whether that will work for you.