-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open log file with Read/Write access #152
Comments
Thanks for the report! I doubt there are any implications to just opening the I'll take a closer look at this one tomorrow and report back 👍 |
No further remarks are mentioned in the .NET docs and the |
After looking some more, I see that all For another reason, I actually did some testing recently with different file modes on Windows, and while Append mode should be giving a hint to the OS, it didn't actually result in any better performance. I think on Windows at least, Append mode is more about access permissions and semantics; I believe writes before the current EOF are prevented, and it will seek to the end of the file automatically. So, this one isn't so clear cut as I thought 😄 @nblumhardt what are your thoughts here? Reading from the underlying stream would be useful, even if only in some niche scenarios. But I wonder if there is perhaps some other route, such as a new hook: string OnFileOpening(); The idea here would be to provide the caller the name of the file before Serilog opens it, so the caller has a chance to do anything they want with it beforehand (read it, delete it, write a header, whatever). |
Tricky! I'd be wary of changing the open flags, at least without spending some time re-validating the atomic append functionality and concurrent-burn-testing it (https://nblumhardt.com/2016/08/atomic-shared-log-file-writes/ ). My instinct says changing flags here will cause downstream effects and should probably be a last resort :-) By string OnFileOpening(); did you mean: Stream OnFileOpening(string path); I think it would be easy in that kind of scheme to write hooks that break other expectations of the sink, later in the process. Not sure how that would come out, I need to give it more thought - no straightforward options, unfortunately. If you're on a tight timeline, @MaxXor, creating a minimal encrypted file sink based on this package will be the quickest and easiest path. |
@nblumhardt void OnFileOpening(string path); It wouldn't return anything, just give the caller the opportunity to work with the file at that path (if it exists). Something like you suggested below would indeed be much more powerful, and make it rather easy to break things 😄 It would also have some crossover with the existing Stream OnFileOpening(string path); Not sure I entirely understand how append mode helps multiple processes to write to the same file though (the very idea of this sounds highly dubious tho!) - are you saying that if multiple processes open with mode, each write automatically locks the file, seeks to the end of the stream, writes, then unlocks the file? I had imagined it just seeked to the end when the stream was first opened 🤷 But aye, I can certainly see that this isn't as simple as it initially looked 😖 @EamonHetherton you've also been involved with hooks, do you have any thoughts on this? |
My use cases to date have all been 1 proccess = 1 log file and I don't have a depth of experience with the nuances of FileMode/FileAccess so not sure I can contribute much. That said, I'd be concerned that if we simply pass the filename back through a hook before we open it to write to, and that the hook consumer was to open the file for read, that the temporal proximity of their open for read and serilog's open for append/write might cause problems? An option at the file sink level to specify file access mode might work; retaining the existing behaviour by default but I guess the surface area of that change is much larger, not just in code but complexity and documentation etc. Feel free to ignore all of the above if I misinterpreted something, its early and i've got a head cold so brain's running at significantly reduced capacity :) |
Any update on this? |
Nothing happening in the short-term on this; forking and modifying for your needs will be the quickest way forward for now, sorry. |
We sadly have exactly the same use-case. Trying to add encryption, but not possible due to not being able to enforce the public override Stream OnFileOpened(Stream underlyingStream, NetEncoding encoding)
{
ArgumentNullException.ThrowIfNull(underlyingStream);
ArgumentNullException.ThrowIfNull(encoding);
if (underlyingStream is { CanRead: false, Position: not 0 })
{
throw new IOException("It's not possible to reuse the log file since this requires ReadWrite permission on the stream.");
}
return CryptoStream.Write(underlyingStream);
} |
Having an |
I am trying to implement encrypted storing of log files. However this is currently not possible with the FileLifecycleHooks class as the
underlyingStream
parameter was opened with FileAccess.Write. I'm trying to get it to work with AES in CBC mode with padding, which requires re-reading of the last block because of the padding if an existing log file is opened.Would it be possible to open the log files with read/write access in this library?
Below is a minimal example which describes the situation:
The text was updated successfully, but these errors were encountered: