Skip to content

Commit 7938c90

Browse files
authored
Merge pull request serilog#1 from serilog/dev
Merge
2 parents 0639f1e + 455c32c commit 7938c90

Some content is hidden

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

54 files changed

+2890
-369
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ insert_final_newline = true
66
indent_style = space
77
indent_size = 4
88

9-
[*.{csproj,json,config,yml}]
9+
[*.{csproj,json,config,yml,props}]
1010
indent_size = 2
1111

1212
[*.sh]

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ bld/
2626
# Uncomment if you have tasks that create the project's static files in wwwroot
2727
#wwwroot/
2828

29+
# Rider cache/options directory
30+
.idea
31+
2932
# MSTest test Results
3033
[Tt]est[Rr]esult*/
3134
[Bb]uild[Ll]og.*

.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.1.300
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 -p:EnableSourceLink=true
27+
if ($suffix) {
28+
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix --no-build
29+
} else {
30+
& dotnet pack -c Release -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

+173-15
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,211 @@
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) [![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)
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-
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://www.nuget.org/packages/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

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.
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+
33+
To avoid bringing down apps with runaway disk usage the file sink **limits file size to 1GB by default**. Once the limit is reached, no further events will be written until the next roll point (see also: [Rolling policies](#rolling-policies) below).
34+
35+
The limit can be changed or removed using the `fileSizeLimitBytes` parameter.
1236

1337
```csharp
1438
.WriteTo.File("log.txt", fileSizeLimitBytes: null)
39+
```
40+
41+
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.
42+
43+
```csharp
44+
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: null)
45+
```
46+
47+
### Rolling policies
48+
49+
To create a log file per day or other time period, specify a `rollingInterval` as shown in the examples above.
50+
51+
To roll when the file reaches `fileSizeLimitBytes`, specify `rollOnFileSizeLimit`:
52+
53+
```csharp
54+
.WriteTo.File("log.txt", rollOnFileSizeLimit: true)
55+
```
56+
57+
This will create a file set like:
58+
59+
```
60+
log.txt
61+
log_001.txt
62+
log_002.txt
63+
```
64+
65+
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.
66+
67+
Old files will be cleaned up as per `retainedFileCountLimit` - the default is 31.
68+
69+
### XML `<appSettings>` configuration
70+
71+
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:
72+
73+
```powershell
74+
Install-Package Serilog.Settings.AppSettings
75+
```
76+
77+
Instead of configuring the logger in code, call `ReadFrom.AppSettings()`:
78+
79+
```csharp
80+
var log = new LoggerConfiguration()
81+
.ReadFrom.AppSettings()
82+
.CreateLogger();
83+
```
84+
85+
In your application's `App.config` or `Web.config` file, specify the file sink assembly and required path format under the `<appSettings>` node:
86+
87+
```xml
88+
<configuration>
89+
<appSettings>
90+
<add key="serilog:using:File" value="Serilog.Sinks.File" />
91+
<add key="serilog:write-to:File.path" value="log.txt" />
1592
```
1693

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.
94+
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:
1895

19-
### `<appSettings>` configuration
96+
```xml
97+
<add key="serilog:write-to:File.fileSizeLimitBytes" value="1234567" />
98+
```
99+
100+
Omitting the `value` will set the parameter to `null`:
101+
102+
```xml
103+
<add key="serilog:write-to:File.fileSizeLimitBytes" />
104+
```
20105

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:
106+
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`:
22107

23108
```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="" />
109+
<add key="serilog:write-to:File.path" value="%APPDATA%\MyApp\log.txt" />
27110
```
28111

29-
### JSON formatting
112+
### JSON `appsettings.json` configuration
30113

31-
To emit JSON, rather than plain text, a formatter can be specified:
114+
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:
115+
116+
```powershell
117+
Install-Package Serilog.Settings.Configuration
118+
```
119+
120+
Instead of configuring the file directly in code, call `ReadFrom.Configuration()`:
32121

33122
```csharp
34-
.WriteTo.File(new JsonFormatter(), "log.txt")
123+
var configuration = new ConfigurationBuilder()
124+
.AddJsonFile("appsettings.json")
125+
.Build();
126+
127+
var logger = new LoggerConfiguration()
128+
.ReadFrom.Configuration(configuration)
129+
.CreateLogger();
35130
```
36131

37-
To configure an alternative formatter in XML `<appSettings>`, specify the formatter's assembly-qualified type name as the setting `value`.
132+
In your `appsettings.json` file, under the `Serilog` node, :
133+
134+
```json
135+
{
136+
"Serilog": {
137+
"WriteTo": [
138+
{ "Name": "File", "Args": { "path": "log.txt", "rollingInterval": "Day" } }
139+
]
140+
}
141+
}
142+
```
143+
144+
See the XML `<appSettings>` example above for a discussion of available `Args` options.
145+
146+
### Controlling event formatting
147+
148+
The file sink creates events in a fixed text format by default:
149+
150+
```
151+
2018-07-06 09:02:17.148 +10:00 [INF] HTTP GET / responded 200 in 1994 ms
152+
```
153+
154+
The format is controlled using an _output template_, which the file configuration method accepts as an `outputTemplate` parameter.
155+
156+
The default format above corresponds to an output template like:
157+
158+
```csharp
159+
.WriteTo.File("log.txt",
160+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
161+
```
162+
163+
##### JSON event formatting
164+
165+
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:
166+
167+
```csharp
168+
// Install-Package Serilog.Formatting.Compact
169+
.WriteTo.File(new CompactJsonFormatter(), "log.txt")
170+
```
38171

39172
### Shared log files
40173

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

43176
```csharp
44177
.WriteTo.File("log.txt", shared: true)
45178
```
46179

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

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

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

196+
### Extensibility
197+
[`FileLifecycleHooks`](https://github.com/serilog/serilog-sinks-file/blob/master/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs) provide an extensibility point that allows hooking into different parts of the life cycle of a log file.
198+
199+
You can create a hook by extending from [`FileLifecycleHooks`](https://github.com/serilog/serilog-sinks-file/blob/master/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs) and overriding the `OnFileOpened` and/or `OnFileDeleting` methods.
200+
201+
- `OnFileOpened` provides access to the underlying stream that log events are written to, before Serilog begins writing events. You can use this to write your own data to the stream (for example, to write a header row), or to wrap the stream in another stream (for example, to add buffering, compression or encryption)
202+
203+
- `OnFileDeleting` provides a means to work with obsolete rolling log files, *before* they are deleted by Serilog's retention mechanism - for example, to archive log files to another location
204+
205+
Available hooks:
206+
207+
- [serilog-sinks-file-header](https://github.com/cocowalla/serilog-sinks-file-header): writes a header to the start of each log file
208+
- [serilog-sinks-file-gzip](https://github.com/cocowalla/serilog-sinks-file-gzip): compresses logs as they are written, using streaming GZIP compression
209+
- [serilog-sinks-file-archive](https://github.com/cocowalla/serilog-sinks-file-archive): archives obsolete rolling log files before they are deleted by Serilog's retention mechanism
210+
53211
_Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._

appveyor.yml

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2015
4-
configuration: Release
5-
install:
6-
- 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"
3+
image:
4+
- Visual Studio 2019
5+
- Ubuntu1804
116
build_script:
127
- ps: ./Build.ps1
8+
for:
9+
-
10+
matrix:
11+
only:
12+
- image: Ubuntu1804
13+
build_script:
14+
- sh build.sh
1315
test: off
1416
artifacts:
1517
- path: artifacts/Serilog.*.nupkg
1618
deploy:
1719
- provider: NuGet
1820
api_key:
19-
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
21+
secure: K3/810hkTO6rd2AEHVkUQAadjGmDREus9k96QHu6hxrA1/wRTuAJemHMKtVVgIvf
2022
skip_symbols: true
2123
on:
2224
branch: /^(master|dev)$/

0 commit comments

Comments
 (0)