Skip to content

Commit 1ec387e

Browse files
authored
Merge pull request #31 from serilog/dev
3.1.0 Release
2 parents 5ebf1d5 + e3f3c66 commit 1ec387e

16 files changed

+108
-25
lines changed

Diff for: src/Serilog.Sinks.RollingFile/RollingFileLoggerConfigurationExtensions.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Serilog.Events;
1919
using Serilog.Formatting;
2020
using Serilog.Formatting.Display;
21+
using Serilog.Sinks.File;
2122
using Serilog.Sinks.RollingFile;
2223

2324
namespace Serilog
@@ -54,6 +55,7 @@ public static class RollingFileLoggerConfigurationExtensions
5455
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
5556
/// is false.</param>
5657
/// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
58+
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
5759
/// <returns>Configuration object allowing method chaining.</returns>
5860
/// <remarks>The file will be written using the UTF-8 encoding without a byte-order mark.</remarks>
5961
public static LoggerConfiguration RollingFile(
@@ -66,11 +68,12 @@ public static LoggerConfiguration RollingFile(
6668
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
6769
LoggingLevelSwitch levelSwitch = null,
6870
bool buffered = false,
69-
bool shared = false)
71+
bool shared = false,
72+
TimeSpan? flushToDiskInterval = null)
7073
{
7174
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
7275
return RollingFile(sinkConfiguration, formatter, pathFormat, restrictedToMinimumLevel, fileSizeLimitBytes,
73-
retainedFileCountLimit, levelSwitch, buffered, shared);
76+
retainedFileCountLimit, levelSwitch, buffered, shared, flushToDiskInterval);
7477
}
7578

7679
/// <summary>
@@ -95,6 +98,7 @@ public static LoggerConfiguration RollingFile(
9598
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
9699
/// is false.</param>
97100
/// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
101+
/// <param name="flushToDiskInterval">If provided, a full disk flush will be performed periodically at the specified interval.</param>
98102
/// <returns>Configuration object allowing method chaining.</returns>
99103
/// <remarks>The file will be written using the UTF-8 encoding without a byte-order mark.</remarks>
100104
public static LoggerConfiguration RollingFile(
@@ -106,16 +110,23 @@ public static LoggerConfiguration RollingFile(
106110
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
107111
LoggingLevelSwitch levelSwitch = null,
108112
bool buffered = false,
109-
bool shared = false)
113+
bool shared = false,
114+
TimeSpan? flushToDiskInterval = null)
110115
{
111116
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
112117
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
113118

114119
if (shared && buffered)
115120
throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
116121

117-
var sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered, shared: shared);
122+
ILogEventSink sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered, shared: shared);
123+
124+
if (flushToDiskInterval.HasValue)
125+
{
126+
sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value);
127+
}
128+
118129
return sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
119130
}
120131
}
121-
}
132+
}

Diff for: src/Serilog.Sinks.RollingFile/Sinks/RollingFile/RollingFileSink.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace Serilog.Sinks.RollingFile
3131
/// the date of the first log entry written to it. Only simple date-based rolling is
3232
/// currently supported.
3333
/// </summary>
34-
public sealed class RollingFileSink : ILogEventSink, IDisposable
34+
public sealed class RollingFileSink : ILogEventSink, IFlushableFileSink, IDisposable
3535
{
3636
readonly TemplatedPathRoller _roller;
3737
readonly ITextFormatter _textFormatter;
@@ -243,5 +243,14 @@ void CloseFile()
243243

244244
_nextCheckpoint = null;
245245
}
246+
247+
/// <inheritdoc />
248+
public void FlushToDisk()
249+
{
250+
lock (_syncRoot)
251+
{
252+
(_currentFile as IFlushableFileSink)?.FlushToDisk();
253+
}
254+
}
246255
}
247-
}
256+
}

Diff for: src/Serilog.Sinks.RollingFile/project.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.1-*",
2+
"version": "3.1.0-*",
33
"description": "The rolling file sink for Serilog - Simple .NET logging with fully-structured events",
44
"authors": [ "Serilog Contributors" ],
55
"packOptions": {
@@ -9,7 +9,7 @@
99
"iconUrl": "http://serilog.net/images/serilog-sink-nuget.png"
1010
},
1111
"dependencies": {
12-
"Serilog.Sinks.File": "3.0.1"
12+
"Serilog.Sinks.File": "3.1.0"
1313
},
1414
"buildOptions": {
1515
"keyFile": "../../assets/Serilog.snk",
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
using System;
2+
using System.Threading;
3+
using Serilog.Sinks.RollingFile.Tests.Support;
24
using Xunit;
35

4-
namespace Serilog.Tests
6+
namespace Serilog.Sinks.RollingFile.Tests
57
{
68
public class RollingFileLoggerConfigurationExtensionsTests
79
{
810
[Fact]
9-
public void BuffferingIsNotAvailableWhenSharingEnabled()
11+
public void BufferingIsNotAvailableWhenSharingEnabled()
1012
{
1113
Assert.Throws<ArgumentException>(() =>
1214
new LoggerConfiguration()
1315
.WriteTo.RollingFile("logs", buffered: true, shared: true));
1416
}
17+
18+
[Fact]
19+
public void SinkCanBeConfiguredAndDisposedWhenFlushIntervalSpecified()
20+
{
21+
using (var temp = TempFolder.ForCaller())
22+
using (var logger = new LoggerConfiguration()
23+
.WriteTo.RollingFile(temp.AllocateFilename(), flushToDiskInterval: TimeSpan.FromMilliseconds(500))
24+
.CreateLogger())
25+
{
26+
logger.Information("Hello, rolling file.");
27+
Thread.Sleep(TimeSpan.FromSeconds(1));
28+
}
29+
}
1530
}
1631
}

Diff for: test/Serilog.Sinks.RollingFile.Tests/RollingFileSinkTests.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using Xunit;
5-
using Serilog;
65
using Serilog.Events;
7-
using Serilog.Sinks.RollingFile;
8-
using Serilog.Tests.Support;
6+
using Serilog.Sinks.RollingFile.Tests.Support;
97

108
namespace Serilog.Sinks.RollingFile.Tests
119
{
@@ -105,7 +103,7 @@ static void TestRollingEventSequence(
105103
}
106104
finally
107105
{
108-
((IDisposable)log).Dispose();
106+
log.Dispose();
109107
verifyWritten(verified);
110108
Directory.Delete(folder, true);
111109
}

Diff for: test/Serilog.Sinks.RollingFile.Tests/Serilog.Sinks.RollingFile.Tests.xproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
88
<PropertyGroup Label="Globals">
99
<ProjectGuid>3c2d8e01-5580-426a-bdd9-ec59cd98e618</ProjectGuid>
10-
<RootNamespace>Serilog.Tests</RootNamespace>
10+
<RootNamespace>Serilog.Sinks.RollingFile.Tests</RootNamespace>
1111
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
1212
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
1313
</PropertyGroup>

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/CollectingSink.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Serilog.Core;
44
using Serilog.Events;
55

6-
namespace Serilog.Tests.Support
6+
namespace Serilog.Sinks.RollingFile.Tests.Support
77
{
88
class CollectingSink : ILogEventSink
99
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/DelegateDisposable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Serilog.Tests.Support
3+
namespace Serilog.Sinks.RollingFile.Tests.Support
44
{
55
public class DelegateDisposable : IDisposable
66
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/DelegatingEnricher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Serilog.Core;
33
using Serilog.Events;
44

5-
namespace Serilog.Tests.Support
5+
namespace Serilog.Sinks.RollingFile.Tests.Support
66
{
77
class DelegatingEnricher : ILogEventEnricher
88
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/DelegatingSink.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Serilog.Core;
33
using Serilog.Events;
44

5-
namespace Serilog.Tests.Support
5+
namespace Serilog.Sinks.RollingFile.Tests.Support
66
{
77
public class DelegatingSink : ILogEventSink
88
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/DisposableLogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Serilog.Core;
44
using Serilog.Events;
55

6-
namespace Serilog.Tests.Support
6+
namespace Serilog.Sinks.RollingFile.Tests.Support
77
{
88
public class DisposableLogger : ILogger, IDisposable
99
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/DisposeTrackingSink.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Serilog.Core;
33
using Serilog.Events;
44

5-
namespace Serilog.Tests.Support
5+
namespace Serilog.Sinks.RollingFile.Tests.Support
66
{
77
class DisposeTrackingSink : ILogEventSink, IDisposable
88
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/Extensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Serilog.Events;
22

3-
namespace Serilog.Tests.Support
3+
namespace Serilog.Sinks.RollingFile.Tests.Support
44
{
55
public static class Extensions
66
{

Diff for: test/Serilog.Sinks.RollingFile.Tests/Support/Some.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Serilog.Events;
66
using Serilog.Parsing;
77

8-
namespace Serilog.Tests.Support
8+
namespace Serilog.Sinks.RollingFile.Tests.Support
99
{
1010
static class Some
1111
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Runtime.CompilerServices;
5+
6+
namespace Serilog.Sinks.RollingFile.Tests.Support
7+
{
8+
class TempFolder : IDisposable
9+
{
10+
static readonly Guid Session = Guid.NewGuid();
11+
12+
readonly string _tempFolder;
13+
14+
public TempFolder(string name)
15+
{
16+
_tempFolder = System.IO.Path.Combine(
17+
Environment.GetEnvironmentVariable("TMP"),
18+
"Serilog.Sinks.RollingFile.Tests",
19+
Session.ToString("n"),
20+
name);
21+
22+
Directory.CreateDirectory(_tempFolder);
23+
}
24+
25+
public string Path => _tempFolder;
26+
27+
public void Dispose()
28+
{
29+
try
30+
{
31+
if (Directory.Exists(_tempFolder))
32+
Directory.Delete(_tempFolder, true);
33+
}
34+
catch (Exception ex)
35+
{
36+
Debug.WriteLine(ex);
37+
}
38+
}
39+
40+
public static TempFolder ForCaller([CallerMemberName] string caller = null)
41+
{
42+
if (caller == null) throw new ArgumentNullException(nameof(caller));
43+
return new TempFolder(caller);
44+
}
45+
46+
public string AllocateFilename(string ext = null)
47+
{
48+
return System.IO.Path.Combine(Path, Guid.NewGuid().ToString("n") + "." + (ext ?? "tmp"));
49+
}
50+
}
51+
}

Diff for: test/Serilog.Sinks.RollingFile.Tests/TemplatedPathRollerTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.IO;
33
using System.Linq;
44
using Xunit;
5-
using Serilog.Sinks.RollingFile;
65

76
namespace Serilog.Sinks.RollingFile.Tests
87
{

0 commit comments

Comments
 (0)