Skip to content

Added an overload of File() that accepts an ITextFormatter #5

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 1 commit into from
Jul 4, 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
39 changes: 39 additions & 0 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using Serilog.Formatting.Json;
using Serilog.Sinks.File;

namespace Serilog
Expand Down Expand Up @@ -57,8 +59,45 @@ public static LoggerConfiguration File(
bool buffered = false)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (path == null) throw new ArgumentNullException(nameof(path));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));

var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered);
}

/// <summary>
/// Write log events to the specified file.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
/// text for the file. If control of regular text formatting is required, use the other
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool)"/>
/// and specify the outputTemplate parameter instead.
/// </param>
/// <param name="path">Path to the file.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
/// For unrestricted growth, pass null. The default is 1 GB.</param>
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
/// is false.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
public static LoggerConfiguration File(
this LoggerSinkConfiguration sinkConfiguration,
ITextFormatter formatter,
string path,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
LoggingLevelSwitch levelSwitch = null,
bool buffered = false)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (path == null) throw new ArgumentNullException(nameof(path));

FileSink sink;
try
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.File/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[assembly: CLSCompliant(true)]

[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" +
[assembly: InternalsVisibleTo("Serilog.Sinks.File.Tests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
Expand Down
3 changes: 1 addition & 2 deletions src/Serilog.Sinks.File/Sinks/File/FileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public sealed class FileSink : ILogEventSink, IDisposable
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
/// <exception cref="IOException"></exception>
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null,
bool buffered = false)
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null, bool buffered = false)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter));
Expand Down
75 changes: 75 additions & 0 deletions test/Serilog.Sinks.File.Tests/Sinks/File/FileSinkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.IO;
using Xunit;
using Serilog.Formatting.Json;
using Serilog.Sinks.File.Tests.Support;
using Serilog.Tests.Support;

namespace Serilog.Sinks.File.Tests
{
public class FileSinkTests
{
[Fact]
public void FileIsWrittenIfNonexistent()
{
using (var tmp = TempFolder.ForCaller())
{
var nonexistent = tmp.AllocateFilename("txt");
var evt = Some.LogEvent("Hello, world!");

using (var sink = new FileSink(nonexistent, new JsonFormatter(), null))
{
sink.Emit(evt);
}

var lines = System.IO.File.ReadAllLines(nonexistent);
Assert.Contains("Hello, world!", lines[0]);
}
}

[Fact]
public void FileIsAppendedToWhenAlreadyCreated()
{
using (var tmp = TempFolder.ForCaller())
{
var path = tmp.AllocateFilename("txt");
var evt = Some.LogEvent("Hello, world!");

using (var sink = new FileSink(path, new JsonFormatter(), null))
{
sink.Emit(evt);
}

using (var sink = new FileSink(path, new JsonFormatter(), null))
{
sink.Emit(evt);
}

var lines = System.IO.File.ReadAllLines(path);
Assert.Contains("Hello, world!", lines[0]);
Assert.Contains("Hello, world!", lines[1]);
}
}

[Fact]
public void WhenLimitIsSpecifiedFileSizeIsRestricted()
{
const int maxBytes = 100;

using (var tmp = TempFolder.ForCaller())
{
var path = tmp.AllocateFilename("txt");
var evt = Some.LogEvent(new string('n', maxBytes + 1));

using (var sink = new FileSink(path, new JsonFormatter(), maxBytes))
{
sink.Emit(evt);
}

var size = new FileInfo(path).Length;
Assert.True(size > 0);
Assert.True(size < maxBytes);
}
}
}
}

91 changes: 0 additions & 91 deletions test/Serilog.Sinks.File.Tests/Sinks/IOFile/FileSinkTests.cs

This file was deleted.

21 changes: 0 additions & 21 deletions test/Serilog.Sinks.File.Tests/Support/CollectingSink.cs

This file was deleted.

24 changes: 0 additions & 24 deletions test/Serilog.Sinks.File.Tests/Support/DelegateDisposable.cs

This file was deleted.

22 changes: 0 additions & 22 deletions test/Serilog.Sinks.File.Tests/Support/DelegatingEnricher.cs

This file was deleted.

33 changes: 0 additions & 33 deletions test/Serilog.Sinks.File.Tests/Support/DelegatingSink.cs

This file was deleted.

Loading