Skip to content

2.1 Release #6

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 12 commits into from
Jul 4, 2016
28 changes: 18 additions & 10 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ Push-Location $PSScriptRoot

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

& dotnet restore
& dotnet restore --no-cache

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

Push-Location src/Serilog.Sinks.File
foreach ($src in ls src/Serilog.*) {
Push-Location $src

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

Pop-Location
Push-Location test/Serilog.Sinks.File.Tests
Pop-Location
}

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 2 }
foreach ($test in ls test/Serilog.*.Tests) {
Push-Location $test

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 2 }

Pop-Location
}

Pop-Location
Pop-Location
7 changes: 5 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2.1.0
- Support alternative `ITextFormatter`s through the configuration API (#4)

2.0.0
- Moved to new project
- DotNet Core support
- Moved to new project
- DotNet Core support
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
# Serilog.Sinks.File
# 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)

The file sink for Serilog.

[![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/)


Writes log events to a text file.
Writes [Serilog](https://serilog.net) events to a text file.

```csharp
var log = new LoggerConfiguration()
.WriteTo.File("log.txt")
.CreateLogger();
```

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.
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.

```csharp
.WriteTo.File("log.txt", fileSizeLimitBytes: null)
```

Or in XML [app-settings format](https://github.com/serilog/serilog/wiki/AppSettings):
> **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.

### `<appSettings>` configuration

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:

```xml
<add key="serilog:write-to:File.path" value="log.txt" />
<add key="serilog:write-to:File.fileSizeLimitBytes" value="" />
```

> **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.
### JSON formatting

To emit JSON, rather than plain text, a formatter can be specified:

```csharp
.WriteTo.File(new JsonFormatter(), "log.txt")
```

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

Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).
_Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._
11 changes: 9 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2015
configuration: Release
install:
Expand All @@ -18,5 +19,11 @@ deploy:
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
skip_symbols: true
on:
branch: /^(dev|master)$/

branch: /^(master|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifact: /Serilog.*\.nupkg/
tag: v$(appveyor_build_version)
on:
branch: master
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
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.File/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "2.0.0",
"description": "The file sink for Serilog",
"version": "2.1.0-*",
"description": "Write Serilog events to a text file in plain or JSON format.",
"authors": [ "Serilog Contributors" ],
"packOptions": {
"tags": [ "serilog", "file", "io" ],
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.

Loading