Skip to content

Commit 2197a31

Browse files
Added tests to demonstrate issue with appending to file sink when using an encoding with preamble and a file size limit
1 parent ffff01a commit 2197a31

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

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

Lines changed: 71 additions & 0 deletions
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+
private 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("Irelevant 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

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

0 commit comments

Comments
 (0)