I'm trying to create a logfile using a MemoryMappedFile. The code looks something like this:
_currentFile = MemoryMappedFile.CreateFromFile(filename, FileMode.Create, filename, MaxSize, MemoryMappedFileAccess.ReadWrite);
_currentFile.GetAccessControl().SetAccessRule(new AccessRule<MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl, AccessControlType.Allow));
_accessor = _currentFile.CreateViewAccessor();
The logging works just fine but when I try to put a tail on the file at the same time, I get a Permission denied
.
I've tried to find an answer as to how you can allow reads on a MemoryMappedFile but I was unable to find a straight answer. So here it goes, is it possible to allow readers to access a MemoryMappedFile? In other words, would it be possible to "tail" a MemoryMappedFile that is being actively written to?
If using a MemoryMappedFile as a log file is a bad idea to begin with. Then I'd also like to hear it. And if this is a stupid question to ask then I apologize.
Hans Passant gave the correct answer to this question so let me quote that here: "The exact moments in time when the operating system flushes memory updates to the file, and the order in which they occur, are completely unpredictable. This makes MMFs efficient. It therefore puts a lock on the file that prevents any process from reading it. Since such a process could not see anything but stale junk. Also the reason why common logging libraries, like log4net, do not offer MMFs as one of their many possible log targets. So, yeah, bad idea."