Skip to content
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

RetryingMultiProcessFileAppender - better init BufferSize #3444

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/NLog/Internal/FileAppenders/BaseFileAppender.cs
Expand Up @@ -163,8 +163,9 @@ protected virtual void Dispose(bool disposing)
/// Creates the file stream.
/// </summary>
/// <param name="allowFileSharedWriting">If set to <c>true</c> sets the file stream to allow shared writing.</param>
/// <param name="overrideBufferSize">If larger than 0 then it will be used instead of the default BufferSize for the FileStream.</param>
/// <returns>A <see cref="FileStream"/> object which can be used to write to the file.</returns>
protected FileStream CreateFileStream(bool allowFileSharedWriting)
protected FileStream CreateFileStream(bool allowFileSharedWriting, int overrideBufferSize = 0)
{
int currentDelay = CreateFileParameters.ConcurrentWriteAttemptDelay;

Expand All @@ -175,7 +176,7 @@ protected FileStream CreateFileStream(bool allowFileSharedWriting)
{
try
{
return TryCreateFileStream(allowFileSharedWriting);
return TryCreateFileStream(allowFileSharedWriting, overrideBufferSize);
}
catch (DirectoryNotFoundException)
{
Expand All @@ -194,7 +195,7 @@ protected FileStream CreateFileStream(bool allowFileSharedWriting)
//if creating a directory failed, don't retry for this message (e.g the ConcurrentWriteAttempts below)
throw new NLogRuntimeException("Could not create directory {0}", directoryName);
}
return TryCreateFileStream(allowFileSharedWriting);
return TryCreateFileStream(allowFileSharedWriting, overrideBufferSize);

}
}
Expand All @@ -217,7 +218,7 @@ protected FileStream CreateFileStream(bool allowFileSharedWriting)

#if !SILVERLIGHT && !MONO && !__IOS__ && !__ANDROID__ && !NETSTANDARD
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Objects are disposed elsewhere")]
private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWriting)
private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWriting, int overrideBufferSize)
{
int fileShare = Win32FileNativeMethods.FILE_SHARE_READ;

Expand Down Expand Up @@ -250,7 +251,7 @@ private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWritin
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

fileStream = new FileStream(handle, FileAccess.Write, CreateFileParameters.BufferSize);
fileStream = new FileStream(handle, FileAccess.Write, overrideBufferSize > 0 ? overrideBufferSize : CreateFileParameters.BufferSize);
fileStream.Seek(0, SeekOrigin.End);
return fileStream;
}
Expand All @@ -266,7 +267,7 @@ private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWritin
}
#endif

private FileStream TryCreateFileStream(bool allowFileSharedWriting)
private FileStream TryCreateFileStream(bool allowFileSharedWriting, int overrideBufferSize)
{
UpdateCreationTime();

Expand All @@ -275,7 +276,7 @@ private FileStream TryCreateFileStream(bool allowFileSharedWriting)
{
if (!CreateFileParameters.ForceManaged && PlatformDetector.IsWin32 && !PlatformDetector.IsMono)
{
return WindowsCreateFile(FileName, allowFileSharedWriting);
return WindowsCreateFile(FileName, allowFileSharedWriting, overrideBufferSize);
}
}
catch (SecurityException)
Expand All @@ -295,7 +296,7 @@ private FileStream TryCreateFileStream(bool allowFileSharedWriting)
FileMode.Append,
FileAccess.Write,
fileShare,
CreateFileParameters.BufferSize);
overrideBufferSize > 0 ? overrideBufferSize : CreateFileParameters.BufferSize);
}

private void UpdateCreationTime()
Expand Down
Expand Up @@ -63,7 +63,8 @@ public RetryingMultiProcessFileAppender(string fileName, ICreateFileParameters p
/// <param name="count">The number of bytes.</param>
public override void Write(byte[] bytes, int offset, int count)
{
using (FileStream fileStream = CreateFileStream(false))
int overrideBufferSize = Math.Min((count / 4096 + 1) * 4096, CreateFileParameters.BufferSize);
using (FileStream fileStream = CreateFileStream(false, overrideBufferSize))
{
fileStream.Write(bytes, offset, count);
}
Expand Down