Skip to content

Commit 6d463c0

Browse files
authored
Merge pull request #37 from serilog/dev
4.0.0 Release
2 parents c8418ed + 3e0510c commit 6d463c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2121
-305
lines changed

.travis.yml

+7-45
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
11
language: csharp
22

3-
#dotnet cli require Ubuntu 14.04
4-
sudo: required
5-
dist: trusty
6-
7-
#dotnet cli require OSX 10.10
8-
osx_image: xcode7.1
9-
10-
addons:
11-
apt:
12-
packages:
13-
- gettext
14-
- libcurl4-openssl-dev
15-
- libicu-dev
16-
- libssl-dev
17-
- libunwind8
18-
- zlib1g
19-
20-
os:
21-
- linux
22-
23-
env:
24-
global:
25-
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
26-
- TMP: /tmp
27-
28-
matrix:
29-
- CLI_VERSION=1.0.0-preview2-003121
30-
- CLI_VERSION=Latest
31-
323
matrix:
33-
allow_failures:
34-
- env: CLI_VERSION=Latest
35-
36-
before_install:
37-
- if test "$TRAVIS_OS_NAME" == "osx"; then brew update; brew install openssl; brew link --force openssl; fi
38-
# Download script to install dotnet cli
39-
- if test "$CLI_OBTAIN_URL" == ""; then export CLI_OBTAIN_URL="https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.sh"; fi
40-
- curl -L --create-dirs $CLI_OBTAIN_URL -o ./scripts/obtain/install.sh
41-
- find ./scripts -name "*.sh" -exec chmod +x {} \;
42-
- export DOTNET_INSTALL_DIR="$PWD/.dotnetcli"
43-
# use bash to workaround bug https://github.com/dotnet/cli/issues/1725
44-
- sudo bash ./scripts/obtain/install.sh --channel "preview" --version "$CLI_VERSION" --install-dir "$DOTNET_INSTALL_DIR" --no-path
45-
# add dotnet to PATH
46-
- export PATH="$DOTNET_INSTALL_DIR:$PATH"
47-
4+
include:
5+
- os: linux
6+
dist: trusty
7+
sudo: required
8+
dotnet: 2.0.0
9+
group: edge
4810
script:
49-
- ./build.sh
11+
- ./build.sh

Build.ps1

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
1+
echo "build: Build started"
2+
13
Push-Location $PSScriptRoot
24

3-
if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse }
5+
if(Test-Path .\artifacts) {
6+
echo "build: Cleaning .\artifacts"
7+
Remove-Item .\artifacts -Force -Recurse
8+
}
49

510
& dotnet restore --no-cache
611

712
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
813
$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"]
14+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
15+
$commitHash = $(git rev-parse --short HEAD)
16+
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]
17+
18+
echo "build: Package version suffix is $suffix"
19+
echo "build: Build version suffix is $buildSuffix"
1020

11-
foreach ($src in ls src/Serilog.*) {
21+
foreach ($src in ls src/*) {
1222
Push-Location $src
1323

14-
& dotnet pack -c Release -o ..\..\.\artifacts --version-suffix=$suffix
24+
echo "build: Packaging project in $src"
25+
26+
& dotnet build -c Release --version-suffix=$buildSuffix
27+
if ($suffix) {
28+
& dotnet pack -c Release --include-source -o ..\..\artifacts --version-suffix=$suffix --no-build
29+
} else {
30+
& dotnet pack -c Release --include-source -o ..\..\artifacts --no-build
31+
}
1532
if($LASTEXITCODE -ne 0) { exit 1 }
1633

1734
Pop-Location
1835
}
1936

20-
foreach ($test in ls test/Serilog.*.Tests) {
37+
foreach ($test in ls test/*.Tests) {
2138
Push-Location $test
2239

40+
echo "build: Testing project in $test"
41+
2342
& dotnet test -c Release
24-
if($LASTEXITCODE -ne 0) { exit 2 }
43+
if($LASTEXITCODE -ne 0) { exit 3 }
2544

2645
Pop-Location
2746
}

README.md

+155-14
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,194 @@
11
# Serilog.Sinks.File [![Build status](https://ci.appveyor.com/api/projects/status/hh9gymy0n6tne46j?svg=true)](https://ci.appveyor.com/project/serilog/serilog-sinks-file) [![Travis build](https://travis-ci.org/serilog/serilog-sinks-file.svg)](https://travis-ci.org/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-
Writes [Serilog](https://serilog.net) events to a text file.
3+
Writes [Serilog](https://serilog.net) events to one or more text files.
4+
5+
### Getting started
6+
7+
Install the [Serilog.Sinks.File](https://nuget.org/serilog/serilog-sinks-file) package from NuGet:
8+
9+
```powershell
10+
Install-Package Serilog.Sinks.File
11+
```
12+
13+
To configure the sink in C# code, call `WriteTo.File()` during logger configuration:
414

515
```csharp
616
var log = new LoggerConfiguration()
7-
.WriteTo.File("log.txt")
17+
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
818
.CreateLogger();
919
```
1020

21+
This will append the time period to the filename, creating a file set like:
22+
23+
```
24+
log20180631.txt
25+
log20180701.txt
26+
log20180702.txt
27+
```
28+
29+
> **Important**: By default, only one process may write to a log file at a given time. See _Shared log files_ below for information on multi-process sharing.
30+
31+
### Limits
32+
1133
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.
1234

1335
```csharp
1436
.WriteTo.File("log.txt", fileSizeLimitBytes: null)
1537
```
1638

17-
> **Important:** By default only one process may use a log file at a given time. See _Shared log files_ below if multi-process logging is required.
39+
For the same reason, only **the most recent 31 files** are retained by default (i.e. one long month). To change or remove this limit, pass the `retainedFileCountLimit` parameter.
40+
41+
```csharp
42+
.WriteTo.RollingFile("log.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: null)
43+
```
44+
45+
### Rolling policies
46+
47+
To create a log file per day or other time period, specify a `rollingInterval` as shown in the examples above.
48+
49+
To roll when the file reaches `fileSizeLimitBytes`, specify `rollOnFileSizeLimit`:
50+
51+
```csharp
52+
.WriteTo.File("log.txt", rollOnFileSizeLimit: true)
53+
```
54+
55+
This will create a file set like:
56+
57+
```
58+
log.txt
59+
log_001.txt
60+
log_002.txt
61+
```
62+
63+
Specifying both `rollingInterval` and `rollOnFileSizeLimit` will cause both policies to be applied, while specifying neither will result in all events being written to a single file.
64+
65+
Old files will be cleaned up as per `retainedFileCountLimit` - the default is 31.
66+
67+
### XML `<appSettings>` configuration
68+
69+
To use the file sink with the [Serilog.Settings.AppSettings](https://github.com/serilog/serilog-settings-appsettings) package, first install that package if you haven't already done so:
70+
71+
```powershell
72+
Install-Package Serilog.Settings.AppSettings
73+
```
74+
75+
Instead of configuring the logger in code, call `ReadFrom.AppSettings()`:
76+
77+
```csharp
78+
var log = new LoggerConfiguration()
79+
.ReadFrom.AppSettings()
80+
.CreateLogger();
81+
```
82+
83+
In your application's `App.config` or `Web.config` file, specify the file sink assembly and required path format under the `<appSettings>` node:
84+
85+
```xml
86+
<configuration>
87+
<appSettings>
88+
<add key="serilog:using:File" value="Serilog.Sinks.File" />
89+
<add key="serilog:write-to:File.pathFormat" value="log.txt" />
90+
```
91+
92+
The parameters that can be set through the `serilog:write-to:File` keys are the method parameters accepted by the `WriteTo.File()` configuration method. This means, for example, that the `fileSizeLimitBytes` parameter can be set with:
93+
94+
```xml
95+
<add key="serilog:write-to:File.fileSizeLimitBytes" value="1234567" />
96+
```
97+
98+
Omitting the `value` will set the parameter to `null`:
1899

19-
### `<appSettings>` configuration
100+
```xml
101+
<add key="serilog:write-to:File.fileSizeLimitBytes" />
102+
```
20103

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:
104+
In XML and JSON configuration formats, environment variables can be used in setting values. This means, for instance, that the log file path can be based on `TMP` or `APPDATA`:
22105

23106
```xml
24-
<add key="serilog:using:File" value="Serilog.Sinks.File" />
25-
<add key="serilog:write-to:File.path" value="log.txt" />
26-
<add key="serilog:write-to:File.fileSizeLimitBytes" value="" />
107+
<add key="serilog:write-to:File.path" value="%APPDATA%\MyApp\log.txt" />
108+
```
109+
110+
### JSON `appsettings.json` configuration
111+
112+
To use the file sink with _Microsoft.Extensions.Configuration_, for example with ASP.NET Core or .NET Core, use the [Serilog.Settings.Configuration](https://github.com/serilog/serilog-settings-configuration) package. First install that package if you have not already done so:
113+
114+
```powershell
115+
Install-Package Serilog.Settings.Configuration
116+
```
117+
118+
Instead of configuring the file directly in code, call `ReadFrom.Configuration()`:
119+
120+
```csharp
121+
var configuration = new ConfigurationBuilder()
122+
.AddJsonFile("appsettings.json")
123+
.Build();
124+
125+
var logger = new LoggerConfiguration()
126+
.ReadFrom.Configuration(configuration)
127+
.CreateLogger();
27128
```
28129

29-
### JSON formatting
130+
In your `appsettings.json` file, under the `Serilog` node, :
30131

31-
To emit JSON, rather than plain text, a formatter can be specified:
132+
```json
133+
{
134+
"Serilog": {
135+
"WriteTo": [
136+
{ "Name": "File", "Args": { "path": "log.txt", "rollingInterval": "Day" } }
137+
]
138+
}
139+
}
140+
```
141+
142+
See the XML `<appSettings>` example above for a discussion of available `Args` options.
143+
144+
### Controlling event formatting
145+
146+
The file sink creates events in a fixed text format by default:
147+
148+
```
149+
2018-07-06 09:02:17.148 +10:00 [INF] HTTP GET / responded 200 in 1994 ms
150+
```
151+
152+
The format is controlled using an _output template_, which the file configuration method accepts as an `outputTemplate` parameter.
153+
154+
The default format above corresponds to an output template like:
32155

33156
```csharp
34-
.WriteTo.File(new JsonFormatter(), "log.txt")
157+
.WriteTo.File("log.txt",
158+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{u3}] {Message:lj}{NewLine}{Exception}")
35159
```
36160

37-
To configure an alternative formatter in XML `<appSettings>`, specify the formatter's assembly-qualified type name as the setting `value`.
161+
##### JSON event formatting
162+
163+
To write events to the file in an alternative format such as [JSON](https://github.com/serilog/serilog-formatting-compact), pass an `ITextFormatter` as the first argument:
164+
165+
```csharp
166+
// Install-Package Serilog.Formatting.Compact
167+
.WriteTo.File(new CompactJsonFormatter(), "log.txt")
168+
```
38169

39170
### Shared log files
40171

41-
Multiple processes can concurrently write to the same log file if the `shared` parameter is set to `true`:
172+
To enable multi-process shared log files, set `shared` to `true`:
42173

43174
```csharp
44175
.WriteTo.File("log.txt", shared: true)
45176
```
46177

178+
### Auditing
179+
180+
The file sink can operate as an audit file through `AuditTo`:
181+
182+
```csharp
183+
.AuditTo.File("audit.txt")
184+
```
185+
186+
Only a limited subset of configuration options are currently available in this mode.
187+
47188
### Performance
48189

49190
By default, the file sink will flush each event written through it to disk. To improve write performance, specifying `buffered: true` will permit the underlying stream to buffer writes.
50191

51-
The [Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async) package can be used to wrap the file sink and perform all disk accss on a background worker thread.
192+
The [Serilog.Sinks.Async](https://github.com/serilog/serilog-sinks-async) package can be used to wrap the file sink and perform all disk access on a background worker thread.
52193

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

appveyor.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2015
3+
image: Visual Studio 2017
44
configuration: Release
55
install:
66
- ps: mkdir -Force ".\build\" | Out-Null
7-
- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
8-
- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
9-
- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 1.0.0-preview2-003121'
10-
- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
117
build_script:
128
- ps: ./Build.ps1
139
test: off

build.sh

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/bin/bash
2-
dotnet restore --no-cache
3-
for path in src/*/project.json; do
4-
dirname="$(dirname "${path}")"
5-
dotnet build ${dirname} -f netstandard1.3 -c Release
2+
dotnet --info
3+
dotnet restore
4+
5+
for path in src/**/*.csproj; do
6+
dotnet build -f netstandard1.3 -c Release ${path}
67
done
78

8-
for path in test/*.Tests/project.json; do
9-
dirname="$(dirname "${path}")"
10-
dotnet build ${dirname} -f netcoreapp1.0 -c Release
11-
dotnet test ${dirname} -f netcoreapp1.0 -c Release
9+
for path in test/*.Tests/*.csproj; do
10+
dotnet test -f netcoreapp2.0 -c Release ${path}
1211
done

example/Sample/Sample.csproj

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.0;net47</TargetFrameworks>
5+
<AssemblyName>Sample</AssemblyName>
6+
<OutputType>Exe</OutputType>
7+
<PackageId>Sample</PackageId>
8+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\Serilog.Sinks.File\Serilog.Sinks.File.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup Condition=" '$(TargetFramework)' == 'net47' ">
16+
<Reference Include="System" />
17+
<Reference Include="Microsoft.CSharp" />
18+
</ItemGroup>
19+
20+
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
21+
</ItemGroup>
22+
23+
</Project>
24+

0 commit comments

Comments
 (0)