Log File Monitor

孤人 提交于 2019-12-05 18:14:09

it depends on how the first app open that file.

i.e when calling CreateFile API to open a file, there is dwShareMode param which tells the api how to open it (if this was given 0, it can't be accessed from other applications IIRC). otherwise there should be no problem with reading from that file. if im not mistaken, to check if that file is being opened read only u can call something like

CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) ;
  1. Download Process Monitor from Sysinternals.
  2. Open the filter dialog and add a "path" filter for your log file.
  3. Start the log-writing application (I'll call this "logwriter").
  4. Look for and click on the event where logwriter does a CreateFile.
  5. Under "Detail", it should have "Desired Access: Generic Write". And it should have "ShareMode: Read", which corresponds to FILE_SHARE_READ in the call to CreateFile. What it means is, "I, logwriter, permit others to read my file".
  6. Now run your log-reading application ("logreader"), and do the same exercise.
  7. The Detail should have "Desired Access: Generic Read". And it should have "ShareMode: Read, Write", which means, "I, logreader, permit others, including logwriter, to read and write to the log file".

Those are the most sensible values, I think, and they will prevent locking. Other combinations may be permissible. There is a table here.

Now, you haven't said what happens when it "does not seem to work in all cases". What to do next will really depend on the details. Hopefully the above will give you enough information to work out what is going wrong.

You won't get a lock conflict because the writing application is very unlikely to have locked the file. Doing what you suggest generally works without problems (it's what the UNIX tail -f command does) and those minor glitches that do occur can be ignored. I've written a couple of log monitoring apps in te past that worked like this, with no problems.

Try using FileSystemWatcher to get events when a file is updated.

A more delphi friendly link

Quite apart from getting the file sharing to work right which may be impossible depending on what the other program requests, some programs will close the file between accesses.

I have had success in the past with my program waiting for the file to become available, then quickly opening it, grabbing the needed data and closing it. At least in DOS an attempt to access a locked file caused a few retries, and I bumped up this setting, so that if the other program tried for the file while I had it they would simply be delayed and never see an error.

I was even able to update the file (I made sure NOT to close it in between!) without the other program ever knowing a thing.

Ugly as sin but we couldn't change the other program so it was the only way to get the job done. It was deployed in-house for years, I never heard a peep from the users of that system. It finally went away when the machinery the other program controlled was retired.

XpoLog will do the trick without changing your env or code, XpoLog log monitor

Avar is right - you are at the mercy of the writing program here. If they are locking the file, then there are a couple of things you can do:

1 - Check for a change in the "last modified" date time - if that changes, then you know something has happened.

2 - If the mod datetime did change, then (depending on the size of the file) it might be good enough to create a copy of the file and check that.

we use "Tail for win32",

i know its not delphi but it might be useful

http://tailforwin32.sourceforge.net/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!