Skip to content

Commit f04c568

Browse files
committed
Tests pass
1 parent 7614553 commit f04c568

File tree

8 files changed

+75
-80
lines changed

8 files changed

+75
-80
lines changed

example/Sample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void Main(string[] args)
1414
var sw = System.Diagnostics.Stopwatch.StartNew();
1515

1616
Log.Logger = new LoggerConfiguration()
17-
.WriteTo.File("log.txt", fileSizeLimitBytes: 1000000, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true)
17+
.WriteTo.File("log.txt")
1818
.CreateLogger();
1919

2020
for (var i = 0; i < 1000000; ++i)

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,10 @@ public static LoggerConfiguration File(
6969
bool shared,
7070
TimeSpan? flushToDiskInterval)
7171
{
72-
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
73-
if (path == null) throw new ArgumentNullException(nameof(path));
74-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
75-
76-
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
77-
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval);
72+
// ReSharper disable once RedundantArgumentDefaultValue
73+
return File(sinkConfiguration, path, restrictedToMinimumLevel, outputTemplate, formatProvider, fileSizeLimitBytes,
74+
levelSwitch, buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false,
75+
null, null);
7876
}
7977

8078
/// <summary>
@@ -112,7 +110,9 @@ public static LoggerConfiguration File(
112110
bool shared,
113111
TimeSpan? flushToDiskInterval)
114112
{
115-
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval);
113+
// ReSharper disable once RedundantArgumentDefaultValue
114+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
115+
buffered, shared, flushToDiskInterval, RollingInterval.Infinite, false, null, null);
116116
}
117117

118118
/// <summary>
@@ -163,7 +163,9 @@ public static LoggerConfiguration File(
163163
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
164164

165165
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
166-
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval, rollingInterval: rollingInterval, rollOnFileSizeLimit: rollOnFileSizeLimit);
166+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes,
167+
levelSwitch, buffered, shared, flushToDiskInterval,
168+
rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit, encoding);
167169
}
168170

169171
/// <summary>
@@ -211,8 +213,7 @@ public static LoggerConfiguration File(
211213
Encoding encoding = null)
212214
{
213215
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
214-
buffered: buffered, shared: shared, flushToDiskInterval: flushToDiskInterval, rollingInterval: rollingInterval,
215-
rollOnFileSizeLimit: rollOnFileSizeLimit, retainedFileCountLimit: retainedFileCountLimit, encoding: encoding);
216+
buffered, false, shared, flushToDiskInterval, encoding, rollingInterval, rollOnFileSizeLimit, retainedFileCountLimit);
216217
}
217218

218219
/// <summary>
@@ -268,24 +269,25 @@ public static LoggerConfiguration File(
268269
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
269270
LoggingLevelSwitch levelSwitch = null)
270271
{
271-
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, null, levelSwitch, false, true);
272+
return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, null, levelSwitch, false, true,
273+
false, null, null, RollingInterval.Infinite, false, null);
272274
}
273275

274276
static LoggerConfiguration ConfigureFile(
275277
this Func<ILogEventSink, LogEventLevel, LoggingLevelSwitch, LoggerConfiguration> addSink,
276278
ITextFormatter formatter,
277279
string path,
278-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
279-
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
280-
LoggingLevelSwitch levelSwitch = null,
281-
bool buffered = false,
282-
bool propagateExceptions = false,
283-
bool shared = false,
284-
TimeSpan? flushToDiskInterval = null,
285-
Encoding encoding = null,
286-
RollingInterval rollingInterval = RollingInterval.Infinite,
287-
bool rollOnFileSizeLimit = false,
288-
int? retainedFileCountLimit = DefaultRetainedFileCountLimit)
280+
LogEventLevel restrictedToMinimumLevel,
281+
long? fileSizeLimitBytes,
282+
LoggingLevelSwitch levelSwitch,
283+
bool buffered,
284+
bool propagateExceptions,
285+
bool shared,
286+
TimeSpan? flushToDiskInterval,
287+
Encoding encoding,
288+
RollingInterval rollingInterval,
289+
bool rollOnFileSizeLimit,
290+
int? retainedFileCountLimit)
289291
{
290292
if (addSink == null) throw new ArgumentNullException(nameof(addSink));
291293
if (formatter == null) throw new ArgumentNullException(nameof(formatter));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Xunit;
66
using System.IO;
77

8-
namespace Serilog.Tests
8+
namespace Serilog.Sinks.File.Tests
99
{
1010
public class FileLoggerConfigurationExtensionsTests
1111
{

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using Xunit;
43
using Serilog.Formatting.Json;
54
using Serilog.Sinks.File.Tests.Support;
65
using Serilog.Tests.Support;
76
using System.Text;
8-
using Serilog.Tests;
7+
8+
#pragma warning disable 618
99

1010
namespace Serilog.Sinks.File.Tests
1111
{
@@ -116,11 +116,10 @@ public void WhenLimitIsSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToF
116116
[Fact]
117117
public void WhenLimitIsNotSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink()
118118
{
119-
long? maxBytes = null;
120119
var encoding = Encoding.UTF8;
121120

122121
Assert.True(encoding.GetPreamble().Length > 0);
123-
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
122+
WriteTwoEventsAndCheckOutputFileLength(null, encoding);
124123
}
125124

126125
[Fact]
@@ -136,11 +135,10 @@ public void WhenLimitIsSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedT
136135
[Fact]
137136
public void WhenLimitIsNotSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink()
138137
{
139-
long? maxBytes = null;
140138
var encoding = new UTF8Encoding(false);
141139

142140
Assert.Equal(0, encoding.GetPreamble().Length);
143-
WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding);
141+
WriteTwoEventsAndCheckOutputFileLength(null, encoding);
144142
}
145143

146144
static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding encoding)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static void TestRollingEventSequence(
116116
IEnumerable<LogEvent> events,
117117
Action<IList<string>> verifyWritten = null)
118118
{
119-
var fileName = Some.String() + "-{Date}.txt";
119+
var fileName = Some.String() + "-.txt";
120120
var folder = Some.TempFolderPath();
121121
var pathFormat = Path.Combine(folder, fileName);
122122

@@ -133,7 +133,7 @@ static void TestRollingEventSequence(
133133
Clock.SetTestDateTimeNow(@event.Timestamp.DateTime);
134134
log.Write(@event);
135135

136-
var expected = pathFormat.Replace("{Date}", @event.Timestamp.ToString("yyyyMMdd"));
136+
var expected = pathFormat.Replace(".txt", @event.Timestamp.ToString("yyyyMMdd") + ".txt");
137137
Assert.True(System.IO.File.Exists(expected));
138138

139139
verified.Add(expected);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using Xunit;
33
using Serilog.Formatting.Json;
44
using Serilog.Sinks.File.Tests.Support;
5-
using Serilog.Tests.Support;
5+
6+
#pragma warning disable 618
67

78
namespace Serilog.Sinks.File.Tests
89
{

test/Serilog.Sinks.File.Tests/Support/Some.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Threading;
56
using Serilog.Events;
67
using Serilog.Parsing;
8+
using Xunit.Sdk;
79

810
namespace Serilog.Sinks.File.Tests.Support
911
{
@@ -41,6 +43,20 @@ public static DateTimeOffset OffsetInstant()
4143
return new DateTimeOffset(Instant());
4244
}
4345

46+
public static LogEvent LogEvent(string messageTemplate, params object[] propertyValues)
47+
{
48+
var log = new LoggerConfiguration().CreateLogger();
49+
MessageTemplate template;
50+
IEnumerable<LogEventProperty> properties;
51+
#pragma warning disable Serilog004 // Constant MessageTemplate verifier
52+
if (!log.BindMessageTemplate(messageTemplate, propertyValues, out template, out properties))
53+
#pragma warning restore Serilog004 // Constant MessageTemplate verifier
54+
{
55+
throw new XunitException("Template could not be bound.");
56+
}
57+
return new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, template, properties);
58+
}
59+
4460
public static LogEvent LogEvent(DateTimeOffset? timestamp = null, LogEventLevel level = LogEventLevel.Information)
4561
{
4662
return new LogEvent(timestamp ?? OffsetInstant(), level,

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

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,24 @@
33
using System.Linq;
44
using Xunit;
55

6-
namespace Serilog.Sinks.RollingFile.Tests
6+
namespace Serilog.Sinks.File.Tests
77
{
8-
public class TemplatedPathRollerTests
8+
public class PathRollerTests
99
{
10-
[Fact]
11-
public void SpecifierCannotBeProvidedInDirectory()
12-
{
13-
var ex = Assert.Throws<ArgumentException>(() => new TemplatedPathRoller("{Date}\\log.txt"));
14-
Assert.True(ex.Message.Contains("directory"));
15-
}
16-
1710
[Fact]
1811
public void TheLogFileIncludesDateToken()
1912
{
20-
var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
13+
var roller = new PathRoller("Logs\\log..txt", RollingInterval.Day);
2114
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
2215
string path;
23-
roller.GetLogFilePath(now, 0, out path);
16+
roller.GetLogFilePath(now, null, out path);
2417
AssertEqualAbsolute("Logs\\log.20130714.txt", path);
2518
}
2619

2720
[Fact]
2821
public void ANonZeroIncrementIsIncludedAndPadded()
2922
{
30-
var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
23+
var roller = new PathRoller("Logs\\log..txt", RollingInterval.Day);
3124
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
3225
string path;
3326
roller.GetLogFilePath(now, 12, out path);
@@ -44,80 +37,65 @@ static void AssertEqualAbsolute(string path1, string path2)
4437
[Fact]
4538
public void TheRollerReturnsTheLogFileDirectory()
4639
{
47-
var roller = new TemplatedPathRoller("Logs\\log.{Date}.txt");
40+
var roller = new PathRoller("Logs\\log..txt", RollingInterval.Day);
4841
AssertEqualAbsolute("Logs", roller.LogFileDirectory);
4942
}
5043

51-
[Fact]
52-
public void IfNoTokenIsSpecifiedDashFollowedByTheDateIsImplied()
53-
{
54-
var roller = new TemplatedPathRoller("Logs\\log.txt");
55-
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
56-
string path;
57-
roller.GetLogFilePath(now, 0, out path);
58-
AssertEqualAbsolute("Logs\\log-20130714.txt", path);
59-
}
60-
6144
[Fact]
6245
public void TheLogFileIsNotRequiredToIncludeAnExtension()
6346
{
64-
var roller = new TemplatedPathRoller("Logs\\log-{Date}");
47+
var roller = new PathRoller("Logs\\log-", RollingInterval.Day);
6548
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
6649
string path;
67-
roller.GetLogFilePath(now, 0, out path);
50+
roller.GetLogFilePath(now, null, out path);
6851
AssertEqualAbsolute("Logs\\log-20130714", path);
6952
}
7053

7154
[Fact]
7255
public void TheLogFileIsNotRequiredToIncludeADirectory()
7356
{
74-
var roller = new TemplatedPathRoller("log-{Date}");
57+
var roller = new PathRoller("log-", RollingInterval.Day);
7558
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
7659
string path;
77-
roller.GetLogFilePath(now, 0, out path);
60+
roller.GetLogFilePath(now, null, out path);
7861
AssertEqualAbsolute("log-20130714", path);
7962
}
8063

8164
[Fact]
8265
public void MatchingExcludesSimilarButNonmatchingFiles()
8366
{
84-
var roller = new TemplatedPathRoller("log-{Date}.txt");
67+
var roller = new PathRoller("log-.txt", RollingInterval.Day);
8568
const string similar1 = "log-0.txt";
8669
const string similar2 = "log-helloyou.txt";
8770
var matched = roller.SelectMatches(new[] { similar1, similar2 });
8871
Assert.Equal(0, matched.Count());
8972
}
9073

91-
[Theory]
92-
[InlineData("Logs\\log-{Date}.txt")]
93-
[InlineData("Logs\\log-{Hour}.txt")]
94-
[InlineData("Logs\\log-{HalfHour}.txt")]
95-
public void TheDirectorSearchPatternUsesWildcardInPlaceOfDate(string template)
74+
[Fact]
75+
public void TheDirectorSearchPatternUsesWildcardInPlaceOfDate()
9676
{
97-
var roller = new TemplatedPathRoller(template);
77+
var roller = new PathRoller("Logs\\log-.txt", RollingInterval.Day);
9878
Assert.Equal("log-*.txt", roller.DirectorySearchPattern);
9979
}
10080

10181
[Theory]
102-
[InlineData("log-{Date}.txt", "log-20131210.txt", "log-20131210_031.txt")]
103-
[InlineData("log-{Hour}.txt", "log-2013121013.txt", "log-2013121013_031.txt")]
104-
[InlineData("log-{HalfHour}.txt", "log-201312100100.txt", "log-201312100230_031.txt")]
105-
public void MatchingSelectsFiles(string template, string zeroth, string thirtyFirst)
82+
[InlineData("log-.txt", "log-20131210.txt", "log-20131210_031.txt", RollingInterval.Day)]
83+
[InlineData("log-.txt", "log-2013121013.txt", "log-2013121013_031.txt", RollingInterval.Hour)]
84+
public void MatchingSelectsFiles(string template, string zeroth, string thirtyFirst, RollingInterval interval)
10685
{
107-
var roller = new TemplatedPathRoller(template);
86+
var roller = new PathRoller(template, interval);
10887
var matched = roller.SelectMatches(new[] { zeroth, thirtyFirst }).ToArray();
109-
Assert.Equal(2, matched.Count());
110-
Assert.Equal(0, matched[0].SequenceNumber);
88+
Assert.Equal(2, matched.Length);
89+
Assert.Equal(null, matched[0].SequenceNumber);
11190
Assert.Equal(31, matched[1].SequenceNumber);
11291
}
11392

11493
[Theory]
115-
[InlineData("log-{Date}.txt", "log-20150101.txt", "log-20141231.txt")]
116-
[InlineData("log-{Hour}.txt", "log-2015010110.txt", "log-2015010109.txt")]
117-
[InlineData("log-{HalfHour}.txt", "log-201501011400.txt", "log-201501011330.txt")]
118-
public void MatchingParsesSubstitutions(string template, string newer, string older)
94+
[InlineData("log-.txt", "log-20150101.txt", "log-20141231.txt", RollingInterval.Day)]
95+
[InlineData("log-.txt", "log-2015010110.txt", "log-2015010109.txt", RollingInterval.Hour)]
96+
public void MatchingParsesSubstitutions(string template, string newer, string older, RollingInterval interval)
11997
{
120-
var roller = new TemplatedPathRoller(template);
98+
var roller = new PathRoller(template, interval);
12199
var matched = roller.SelectMatches(new[] { older, newer }).OrderByDescending(m => m.DateTime).Select(m => m.Filename).ToArray();
122100
Assert.Equal(new[] { newer, older }, matched);
123101
}

0 commit comments

Comments
 (0)