Skip to content

Commit

Permalink
Merge pull request #25 from serilog/dev
Browse files Browse the repository at this point in the history
3.1.1 Release
  • Loading branch information
nblumhardt authored Nov 15, 2016
2 parents 36b3217 + 1b029c8 commit 7be22d6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
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);
}
}
}

0 comments on commit 7be22d6

Please sign in to comment.