Skip to content

3.1.0 Release #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using Serilog.Sinks.File;
using Serilog.Sinks.RollingFile;

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

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

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

var sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered, shared: shared);
ILogEventSink sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit, buffered: buffered, shared: shared);

if (flushToDiskInterval.HasValue)
{
sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value);
}

return sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
}
}
}
}
13 changes: 11 additions & 2 deletions src/Serilog.Sinks.RollingFile/Sinks/RollingFile/RollingFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Serilog.Sinks.RollingFile
/// the date of the first log entry written to it. Only simple date-based rolling is
/// currently supported.
/// </summary>
public sealed class RollingFileSink : ILogEventSink, IDisposable
public sealed class RollingFileSink : ILogEventSink, IFlushableFileSink, IDisposable
{
readonly TemplatedPathRoller _roller;
readonly ITextFormatter _textFormatter;
Expand Down Expand Up @@ -243,5 +243,14 @@ void CloseFile()

_nextCheckpoint = null;
}

/// <inheritdoc />
public void FlushToDisk()
{
lock (_syncRoot)
{
(_currentFile as IFlushableFileSink)?.FlushToDisk();
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.RollingFile/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.1-*",
"version": "3.1.0-*",
"description": "The rolling file sink for Serilog - Simple .NET logging with fully-structured events",
"authors": [ "Serilog Contributors" ],
"packOptions": {
Expand All @@ -9,7 +9,7 @@
"iconUrl": "http://serilog.net/images/serilog-sink-nuget.png"
},
"dependencies": {
"Serilog.Sinks.File": "3.0.1"
"Serilog.Sinks.File": "3.1.0"
},
"buildOptions": {
"keyFile": "../../assets/Serilog.snk",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
using System;
using System.Threading;
using Serilog.Sinks.RollingFile.Tests.Support;
using Xunit;

namespace Serilog.Tests
namespace Serilog.Sinks.RollingFile.Tests
{
public class RollingFileLoggerConfigurationExtensionsTests
{
[Fact]
public void BuffferingIsNotAvailableWhenSharingEnabled()
public void BufferingIsNotAvailableWhenSharingEnabled()
{
Assert.Throws<ArgumentException>(() =>
new LoggerConfiguration()
.WriteTo.RollingFile("logs", buffered: true, shared: true));
}

[Fact]
public void SinkCanBeConfiguredAndDisposedWhenFlushIntervalSpecified()
{
using (var temp = TempFolder.ForCaller())
using (var logger = new LoggerConfiguration()
.WriteTo.RollingFile(temp.AllocateFilename(), flushToDiskInterval: TimeSpan.FromMilliseconds(500))
.CreateLogger())
{
logger.Information("Hello, rolling file.");
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
}
6 changes: 2 additions & 4 deletions test/Serilog.Sinks.RollingFile.Tests/RollingFileSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using System.Collections.Generic;
using System.IO;
using Xunit;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.RollingFile;
using Serilog.Tests.Support;
using Serilog.Sinks.RollingFile.Tests.Support;

namespace Serilog.Sinks.RollingFile.Tests
{
Expand Down Expand Up @@ -105,7 +103,7 @@ static void TestRollingEventSequence(
}
finally
{
((IDisposable)log).Dispose();
log.Dispose();
verifyWritten(verified);
Directory.Delete(folder, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>3c2d8e01-5580-426a-bdd9-ec59cd98e618</ProjectGuid>
<RootNamespace>Serilog.Tests</RootNamespace>
<RootNamespace>Serilog.Sinks.RollingFile.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
class CollectingSink : ILogEventSink
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
public class DelegateDisposable : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
class DelegatingEnricher : ILogEventEnricher
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
public class DelegatingSink : ILogEventSink
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
public class DisposableLogger : ILogger, IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
class DisposeTrackingSink : ILogEventSink, IDisposable
{
Expand Down
2 changes: 1 addition & 1 deletion test/Serilog.Sinks.RollingFile.Tests/Support/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Serilog.Events;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
public static class Extensions
{
Expand Down
2 changes: 1 addition & 1 deletion test/Serilog.Sinks.RollingFile.Tests/Support/Some.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Serilog.Events;
using Serilog.Parsing;

namespace Serilog.Tests.Support
namespace Serilog.Sinks.RollingFile.Tests.Support
{
static class Some
{
Expand Down
51 changes: 51 additions & 0 deletions test/Serilog.Sinks.RollingFile.Tests/Support/TempFolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;

namespace Serilog.Sinks.RollingFile.Tests.Support
{
class TempFolder : IDisposable
{
static readonly Guid Session = Guid.NewGuid();

readonly string _tempFolder;

public TempFolder(string name)
{
_tempFolder = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("TMP"),
"Serilog.Sinks.RollingFile.Tests",
Session.ToString("n"),
name);

Directory.CreateDirectory(_tempFolder);
}

public string Path => _tempFolder;

public void Dispose()
{
try
{
if (Directory.Exists(_tempFolder))
Directory.Delete(_tempFolder, true);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}

public static TempFolder ForCaller([CallerMemberName] string caller = null)
{
if (caller == null) throw new ArgumentNullException(nameof(caller));
return new TempFolder(caller);
}

public string AllocateFilename(string ext = null)
{
return System.IO.Path.Combine(Path, Guid.NewGuid().ToString("n") + "." + (ext ?? "tmp"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using System.Linq;
using Xunit;
using Serilog.Sinks.RollingFile;

namespace Serilog.Sinks.RollingFile.Tests
{
Expand Down