Skip to content

Commit 0cbc2c3

Browse files
authored
Merge pull request #6 from serilog/dev
2.1 Release
2 parents bc937d4 + cd8d2b9 commit 0cbc2c3

20 files changed

+233
-754
lines changed

Build.ps1

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@ Push-Location $PSScriptRoot
22

33
if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse }
44

5-
& dotnet restore
5+
& dotnet restore --no-cache
66

7-
$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
7+
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
8+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
9+
$suffix = @{ $true = ""; $false = "$branch-$revision"}[$branch -eq "master" -and $revision -ne "local"]
810

9-
Push-Location src/Serilog.Sinks.File
11+
foreach ($src in ls src/Serilog.*) {
12+
Push-Location $src
1013

11-
& dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$revision
12-
if($LASTEXITCODE -ne 0) { exit 1 }
14+
& dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$suffix
15+
if($LASTEXITCODE -ne 0) { exit 1 }
1316

14-
Pop-Location
15-
Push-Location test/Serilog.Sinks.File.Tests
17+
Pop-Location
18+
}
1619

17-
& dotnet test -c Release
18-
if($LASTEXITCODE -ne 0) { exit 2 }
20+
foreach ($test in ls test/Serilog.*.Tests) {
21+
Push-Location $test
22+
23+
& dotnet test -c Release
24+
if($LASTEXITCODE -ne 0) { exit 2 }
25+
26+
Pop-Location
27+
}
1928

20-
Pop-Location
2129
Pop-Location

CHANGES.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2.1.0
2+
- Support alternative `ITextFormatter`s through the configuration API (#4)
3+
14
2.0.0
2-
- Moved to new project
3-
- DotNet Core support
5+
- Moved to new project
6+
- DotNet Core support

README.md

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
# Serilog.Sinks.File
1+
# Serilog.Sinks.File [![Build status](https://ci.appveyor.com/api/projects/status/hh9gymy0n6tne46j?svg=true)](https://ci.appveyor.com/project/serilog/serilog-sinks-file) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Sinks.File.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.File/) [![Documentation](https://img.shields.io/badge/docs-wiki-yellow.svg)](https://github.com/serilog/serilog/wiki) [![Join the chat at https://gitter.im/serilog/serilog](https://img.shields.io/gitter/room/serilog/serilog.svg)](https://gitter.im/serilog/serilog)
22

3-
The file sink for Serilog.
4-
5-
[![Build status](https://ci.appveyor.com/api/projects/status/hh9gymy0n6tne46j?svg=true)](https://ci.appveyor.com/project/serilog/serilog-sinks-file) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Sinks.File.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.File/)
6-
7-
8-
Writes log events to a text file.
3+
Writes [Serilog](https://serilog.net) events to a text file.
94

105
```csharp
116
var log = new LoggerConfiguration()
127
.WriteTo.File("log.txt")
138
.CreateLogger();
149
```
1510

16-
To avoid sinking apps with runaway disk usage the file sink **limits file size to 1GB by default**. The limit can be increased or removed using the `fileSizeLimitBytes` parameter.
11+
To avoid bringing down apps with runaway disk usage the file sink **limits file size to 1GB by default**. The limit can be increased or removed using the `fileSizeLimitBytes` parameter.
1712

1813
```csharp
1914
.WriteTo.File("log.txt", fileSizeLimitBytes: null)
2015
```
2116

22-
Or in XML [app-settings format](https://github.com/serilog/serilog/wiki/AppSettings):
17+
> **Important:** Only one process may write to a log file at a given time. For multi-process scenarios, either use separate files or one of the non-file-based sinks.
18+
19+
### `<appSettings>` configuration
20+
21+
The sink can be configured in XML [app-settings format](https://github.com/serilog/serilog/wiki/AppSettings) if the _Serilog.Settings.AppSettings_ package is in use:
2322

2423
```xml
2524
<add key="serilog:write-to:File.path" value="log.txt" />
25+
<add key="serilog:write-to:File.fileSizeLimitBytes" value="" />
2626
```
2727

28-
> **Important:** Only one process may write to a log file at a given time. For multi-process scenarios, either use separate files or one of the non-file-based sinks.
28+
### JSON formatting
29+
30+
To emit JSON, rather than plain text, a formatter can be specified:
31+
32+
```csharp
33+
.WriteTo.File(new JsonFormatter(), "log.txt")
34+
```
2935

30-
* [Documentation](https://github.com/serilog/serilog/wiki)
36+
To configure an alternative formatter in XML `<appSettings>`, specify the formatter's assembly-qualified type name as the setting `value`.
3137

32-
Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).
38+
_Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._

appveyor.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
version: '{build}'
2+
skip_tags: true
23
image: Visual Studio 2015
34
configuration: Release
45
install:
@@ -18,5 +19,11 @@ deploy:
1819
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
1920
skip_symbols: true
2021
on:
21-
branch: /^(dev|master)$/
22-
22+
branch: /^(master|dev)$/
23+
- provider: GitHub
24+
auth_token:
25+
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
26+
artifact: /Serilog.*\.nupkg/
27+
tag: v$(appveyor_build_version)
28+
on:
29+
branch: master

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

+39
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

+1-1
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

+1-2
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));

src/Serilog.Sinks.File/project.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"version": "2.0.0",
3-
"description": "The file sink for Serilog",
2+
"version": "2.1.0-*",
3+
"description": "Write Serilog events to a text file in plain or JSON format.",
44
"authors": [ "Serilog Contributors" ],
55
"packOptions": {
66
"tags": [ "serilog", "file", "io" ],
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

-91
This file was deleted.

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

-21
This file was deleted.

0 commit comments

Comments
 (0)