Skip to content

3.1.1 Release #25

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

Merged
merged 5 commits into from
Nov 15, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.File/Sinks/File/WriteCountingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void Write(byte[] buffer, int offset, int count)

public override void Flush() => _stream.Flush();
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => true;
public override long Length => _stream.Length;

Expand All @@ -60,7 +60,7 @@ public override long Position

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
throw new InvalidOperationException($"Seek operations are not available through `{nameof(WriteCountingStream)}`.");
}

public override void SetLength(long value)
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.File/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.1.0-*",
{
"version": "3.1.1-*",
"description": "Write Serilog events to a text file in plain or JSON format.",
"authors": [ "Serilog Contributors" ],
"packOptions": {
Expand Down
71 changes: 71 additions & 0 deletions test/Serilog.Sinks.File.Tests/FileSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Serilog.Formatting.Json;
using Serilog.Sinks.File.Tests.Support;
using Serilog.Tests.Support;
using System.Text;
using Serilog.Tests;

namespace Serilog.Sinks.File.Tests
{
Expand Down Expand Up @@ -99,6 +101,75 @@ public void WhenLimitIsNotSpecifiedFileSizeIsNotRestricted()
Assert.True(size > maxBytes * 2);
}
}


[Fact]
public void WhenLimitIsSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink()
{
long? maxBytes = 5000;
var encoding = Encoding.UTF8;

Assert.True(encoding.GetPreamble().Length > 0);
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
}

[Fact]
public void WhenLimitIsNotSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink()
{
long? maxBytes = null;
var encoding = Encoding.UTF8;

Assert.True(encoding.GetPreamble().Length > 0);
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
}

[Fact]
public void WhenLimitIsSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink()
{
long? maxBytes = 5000;
var encoding = new UTF8Encoding(false);

Assert.Equal(0, encoding.GetPreamble().Length);
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
}

[Fact]
public void WhenLimitIsNotSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink()
{
long? maxBytes = null;
var encoding = new UTF8Encoding(false);

Assert.Equal(0, encoding.GetPreamble().Length);
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
}

static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding encoding)
{
using (var tmp = TempFolder.ForCaller())
{
var path = tmp.AllocateFilename("txt");
var evt = Some.LogEvent("Irrelevant as it will be replaced by the formatter");
var actualEventOutput = "x";
var formatter = new FixedOutputFormatter(actualEventOutput);
var eventOuputLength = encoding.GetByteCount(actualEventOutput);

using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding))
{
sink.Emit(evt);
}
var size = new FileInfo(path).Length;
Assert.Equal(encoding.GetPreamble().Length + eventOuputLength, size);

//write a second event to the same file
using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding))
{
sink.Emit(evt);
}

size = new FileInfo(path).Length;
Assert.Equal(encoding.GetPreamble().Length + eventOuputLength * 2, size);
}
}
}
}

21 changes: 21 additions & 0 deletions test/Serilog.Sinks.File.Tests/Support/FixedOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Serilog.Formatting;
using Serilog.Events;
using System.IO;

namespace Serilog.Tests.Support
{
public class FixedOutputFormatter : ITextFormatter
{
string _substitutionText;

public FixedOutputFormatter(string substitutionText)
{
_substitutionText = substitutionText;
}

public void Format(LogEvent logEvent, TextWriter output)
{
output.Write(_substitutionText);
}
}
}