Skip to content

Commit f0cfa5b

Browse files
authored
Merge pull request #5 from nblumhardt/dev
Added an overload of File() that accepts an ITextFormatter
2 parents 74f39a1 + 358aff4 commit f0cfa5b

File tree

14 files changed

+181
-725
lines changed

14 files changed

+181
-725
lines changed

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
using Serilog.Core;
1818
using Serilog.Debugging;
1919
using Serilog.Events;
20+
using Serilog.Formatting;
2021
using Serilog.Formatting.Display;
22+
using Serilog.Formatting.Json;
2123
using Serilog.Sinks.File;
2224

2325
namespace Serilog
@@ -57,8 +59,45 @@ public static LoggerConfiguration File(
5759
bool buffered = false)
5860
{
5961
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
62+
if (path == null) throw new ArgumentNullException(nameof(path));
6063
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
64+
6165
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
66+
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch, buffered);
67+
}
68+
69+
/// <summary>
70+
/// Write log events to the specified file.
71+
/// </summary>
72+
/// <param name="sinkConfiguration">Logger sink configuration.</param>
73+
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
74+
/// text for the file. If control of regular text formatting is required, use the other
75+
/// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool)"/>
76+
/// and specify the outputTemplate parameter instead.
77+
/// </param>
78+
/// <param name="path">Path to the file.</param>
79+
/// <param name="restrictedToMinimumLevel">The minimum level for
80+
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
81+
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
82+
/// to be changed at runtime.</param>
83+
/// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
84+
/// For unrestricted growth, pass null. The default is 1 GB.</param>
85+
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
86+
/// is false.</param>
87+
/// <returns>Configuration object allowing method chaining.</returns>
88+
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
89+
public static LoggerConfiguration File(
90+
this LoggerSinkConfiguration sinkConfiguration,
91+
ITextFormatter formatter,
92+
string path,
93+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
94+
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
95+
LoggingLevelSwitch levelSwitch = null,
96+
bool buffered = false)
97+
{
98+
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
99+
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
100+
if (path == null) throw new ArgumentNullException(nameof(path));
62101

63102
FileSink sink;
64103
try

src/Serilog.Sinks.File/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[assembly: CLSCompliant(true)]
88

9-
[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=" +
9+
[assembly: InternalsVisibleTo("Serilog.Sinks.File.Tests, PublicKey=" +
1010
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
1111
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
1212
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +

src/Serilog.Sinks.File/Sinks/File/FileSink.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public sealed class FileSink : ILogEventSink, IDisposable
4444
/// <returns>Configuration object allowing method chaining.</returns>
4545
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
4646
/// <exception cref="IOException"></exception>
47-
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null,
48-
bool buffered = false)
47+
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null, bool buffered = false)
4948
{
5049
if (path == null) throw new ArgumentNullException(nameof(path));
5150
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter));
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.IO;
2+
using Xunit;
3+
using Serilog.Formatting.Json;
4+
using Serilog.Sinks.File.Tests.Support;
5+
using Serilog.Tests.Support;
6+
7+
namespace Serilog.Sinks.File.Tests
8+
{
9+
public class FileSinkTests
10+
{
11+
[Fact]
12+
public void FileIsWrittenIfNonexistent()
13+
{
14+
using (var tmp = TempFolder.ForCaller())
15+
{
16+
var nonexistent = tmp.AllocateFilename("txt");
17+
var evt = Some.LogEvent("Hello, world!");
18+
19+
using (var sink = new FileSink(nonexistent, new JsonFormatter(), null))
20+
{
21+
sink.Emit(evt);
22+
}
23+
24+
var lines = System.IO.File.ReadAllLines(nonexistent);
25+
Assert.Contains("Hello, world!", lines[0]);
26+
}
27+
}
28+
29+
[Fact]
30+
public void FileIsAppendedToWhenAlreadyCreated()
31+
{
32+
using (var tmp = TempFolder.ForCaller())
33+
{
34+
var path = tmp.AllocateFilename("txt");
35+
var evt = Some.LogEvent("Hello, world!");
36+
37+
using (var sink = new FileSink(path, new JsonFormatter(), null))
38+
{
39+
sink.Emit(evt);
40+
}
41+
42+
using (var sink = new FileSink(path, new JsonFormatter(), null))
43+
{
44+
sink.Emit(evt);
45+
}
46+
47+
var lines = System.IO.File.ReadAllLines(path);
48+
Assert.Contains("Hello, world!", lines[0]);
49+
Assert.Contains("Hello, world!", lines[1]);
50+
}
51+
}
52+
53+
[Fact]
54+
public void WhenLimitIsSpecifiedFileSizeIsRestricted()
55+
{
56+
const int maxBytes = 100;
57+
58+
using (var tmp = TempFolder.ForCaller())
59+
{
60+
var path = tmp.AllocateFilename("txt");
61+
var evt = Some.LogEvent(new string('n', maxBytes + 1));
62+
63+
using (var sink = new FileSink(path, new JsonFormatter(), maxBytes))
64+
{
65+
sink.Emit(evt);
66+
}
67+
68+
var size = new FileInfo(path).Length;
69+
Assert.True(size > 0);
70+
Assert.True(size < maxBytes);
71+
}
72+
}
73+
}
74+
}
75+

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

Lines changed: 0 additions & 91 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)