Skip to content

Commit 7be22d6

Browse files
authored
Merge pull request #25 from serilog/dev
3.1.1 Release
2 parents 36b3217 + 1b029c8 commit 7be22d6

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

src/Serilog.Sinks.File/Sinks/File/WriteCountingStream.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override void Write(byte[] buffer, int offset, int count)
4747

4848
public override void Flush() => _stream.Flush();
4949
public override bool CanRead => false;
50-
public override bool CanSeek => false;
50+
public override bool CanSeek => _stream.CanSeek;
5151
public override bool CanWrite => true;
5252
public override long Length => _stream.Length;
5353

@@ -60,7 +60,7 @@ public override long Position
6060

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

6666
public override void SetLength(long value)

src/Serilog.Sinks.File/project.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{
2-
"version": "3.1.0-*",
1+
{
2+
"version": "3.1.1-*",
33
"description": "Write Serilog events to a text file in plain or JSON format.",
44
"authors": [ "Serilog Contributors" ],
55
"packOptions": {

test/Serilog.Sinks.File.Tests/FileSinkTests.cs

+71
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Serilog.Formatting.Json;
55
using Serilog.Sinks.File.Tests.Support;
66
using Serilog.Tests.Support;
7+
using System.Text;
8+
using Serilog.Tests;
79

810
namespace Serilog.Sinks.File.Tests
911
{
@@ -99,6 +101,75 @@ public void WhenLimitIsNotSpecifiedFileSizeIsNotRestricted()
99101
Assert.True(size > maxBytes * 2);
100102
}
101103
}
104+
105+
106+
[Fact]
107+
public void WhenLimitIsSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink()
108+
{
109+
long? maxBytes = 5000;
110+
var encoding = Encoding.UTF8;
111+
112+
Assert.True(encoding.GetPreamble().Length > 0);
113+
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
114+
}
115+
116+
[Fact]
117+
public void WhenLimitIsNotSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink()
118+
{
119+
long? maxBytes = null;
120+
var encoding = Encoding.UTF8;
121+
122+
Assert.True(encoding.GetPreamble().Length > 0);
123+
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
124+
}
125+
126+
[Fact]
127+
public void WhenLimitIsSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink()
128+
{
129+
long? maxBytes = 5000;
130+
var encoding = new UTF8Encoding(false);
131+
132+
Assert.Equal(0, encoding.GetPreamble().Length);
133+
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
134+
}
135+
136+
[Fact]
137+
public void WhenLimitIsNotSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink()
138+
{
139+
long? maxBytes = null;
140+
var encoding = new UTF8Encoding(false);
141+
142+
Assert.Equal(0, encoding.GetPreamble().Length);
143+
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
144+
}
145+
146+
static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding encoding)
147+
{
148+
using (var tmp = TempFolder.ForCaller())
149+
{
150+
var path = tmp.AllocateFilename("txt");
151+
var evt = Some.LogEvent("Irrelevant as it will be replaced by the formatter");
152+
var actualEventOutput = "x";
153+
var formatter = new FixedOutputFormatter(actualEventOutput);
154+
var eventOuputLength = encoding.GetByteCount(actualEventOutput);
155+
156+
using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding))
157+
{
158+
sink.Emit(evt);
159+
}
160+
var size = new FileInfo(path).Length;
161+
Assert.Equal(encoding.GetPreamble().Length + eventOuputLength, size);
162+
163+
//write a second event to the same file
164+
using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding))
165+
{
166+
sink.Emit(evt);
167+
}
168+
169+
size = new FileInfo(path).Length;
170+
Assert.Equal(encoding.GetPreamble().Length + eventOuputLength * 2, size);
171+
}
172+
}
102173
}
103174
}
104175

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Serilog.Formatting;
2+
using Serilog.Events;
3+
using System.IO;
4+
5+
namespace Serilog.Tests.Support
6+
{
7+
public class FixedOutputFormatter : ITextFormatter
8+
{
9+
string _substitutionText;
10+
11+
public FixedOutputFormatter(string substitutionText)
12+
{
13+
_substitutionText = substitutionText;
14+
}
15+
16+
public void Format(LogEvent logEvent, TextWriter output)
17+
{
18+
output.Write(_substitutionText);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)