-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow FileLifecycleHooks to change the length of the stream. Resolves #…
- Loading branch information
1 parent
edd4ba9
commit a2fa447
Showing
5 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Serilog.Sinks.File.Tests.Support | ||
{ | ||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Demonstrates the use of <seealso cref="T:Serilog.FileLifecycleHooks" />, by emptying the file before it's written to | ||
/// </summary> | ||
public class TruncateFileHook : FileLifecycleHooks | ||
{ | ||
public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding) | ||
{ | ||
underlyingStream.SetLength(0); | ||
return base.OnFileOpened(underlyingStream, encoding); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.IO; | ||
using System.Text; | ||
using Serilog.Sinks.File.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.File.Tests | ||
{ | ||
public class WriteCountingStreamTests | ||
{ | ||
[Fact] | ||
public void CountedLengthIsResetToStreamLengthIfNewSizeIsSmaller() | ||
{ | ||
// If we counted 10 bytes written and SetLength was called with a smaller length (e.g. 5) | ||
// we adjust the counter to the new byte count of the file to reflect reality | ||
|
||
using (var tmp = TempFolder.ForCaller()) | ||
{ | ||
var path = tmp.AllocateFilename("txt"); | ||
|
||
long streamLengthAfterSetLength; | ||
long countedLengthAfterSetLength; | ||
|
||
using (var fileStream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)) | ||
using (var countingStream = new WriteCountingStream(fileStream)) | ||
using (var writer = new StreamWriter(countingStream, new UTF8Encoding(false))) | ||
{ | ||
writer.WriteLine("Hello, world!"); | ||
writer.Flush(); | ||
|
||
countingStream.SetLength(5); | ||
streamLengthAfterSetLength = countingStream.Length; | ||
countedLengthAfterSetLength = countingStream.CountedLength; | ||
} | ||
|
||
Assert.Equal(5, streamLengthAfterSetLength); | ||
Assert.Equal(5, countedLengthAfterSetLength); | ||
|
||
var lines = System.IO.File.ReadAllLines(path); | ||
|
||
Assert.Single(lines); | ||
Assert.Equal("Hello", lines[0]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CountedLengthRemainsTheSameIfNewSizeIsLarger() | ||
{ | ||
// If we counted 10 bytes written and SetLength was called with a larger length (e.g. 100) | ||
// we leave the counter intact because our position on the stream remains the same... The | ||
// file just grew larger in size | ||
|
||
using (var tmp = TempFolder.ForCaller()) | ||
{ | ||
var path = tmp.AllocateFilename("txt"); | ||
|
||
long streamLengthBeforeSetLength; | ||
long streamLengthAfterSetLength; | ||
long countedLengthAfterSetLength; | ||
|
||
using (var fileStream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)) | ||
using (var countingStream = new WriteCountingStream(fileStream)) | ||
using (var writer = new StreamWriter(countingStream, new UTF8Encoding(false))) | ||
{ | ||
writer.WriteLine("Hello, world!"); | ||
writer.Flush(); | ||
|
||
streamLengthBeforeSetLength = countingStream.CountedLength; | ||
countingStream.SetLength(100); | ||
streamLengthAfterSetLength = countingStream.Length; | ||
countedLengthAfterSetLength = countingStream.CountedLength; | ||
} | ||
|
||
Assert.Equal(100, streamLengthAfterSetLength); | ||
Assert.Equal(streamLengthBeforeSetLength, countedLengthAfterSetLength); | ||
|
||
var lines = System.IO.File.ReadAllLines(path); | ||
|
||
Assert.Equal(2, lines.Length); | ||
Assert.Equal("Hello, world!", lines[0]); | ||
} | ||
} | ||
} | ||
} |