Skip to content

Commit f3abfb8

Browse files
Merge pull request #1 from serilog/dev
Merge
2 parents 2fa2921 + 9a0257e commit f3abfb8

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

+2917
-324
lines changed

.editorconfig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
root=true
1+
root = true
22

33
[*]
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true
46
indent_style = space
57
indent_size = 4
8+
9+
[*.{csproj,json,config,yml,props}]
10+
indent_size = 2
11+
12+
[*.sh]
13+
end_of_line = lf
14+
15+
[*.{cmd, bat}]
16+
end_of_line = crlf

.gitignore

Lines changed: 4 additions & 0 deletions
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.*
@@ -234,3 +237,4 @@ _Pvt_Extensions
234237

235238
# FAKE - F# Make
236239
.fake/
240+
example/Sample/log.txt

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: csharp
2+
3+
matrix:
4+
include:
5+
- os: linux
6+
dist: trusty
7+
sudo: required
8+
dotnet: 2.1.300
9+
group: edge
10+
script:
11+
- ./build.sh

Build.ps1

Lines changed: 25 additions & 6 deletions
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

Lines changed: 158 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,196 @@
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) [![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" />
92+
```
93+
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:
95+
96+
```xml
97+
<add key="serilog:write-to:File.fileSizeLimitBytes" value="1234567" />
1598
```
1699

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.
100+
Omitting the `value` will set the parameter to `null`:
18101

19-
### `<appSettings>` configuration
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" />
110+
```
111+
112+
### JSON `appsettings.json` configuration
113+
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()`:
121+
122+
```csharp
123+
var configuration = new ConfigurationBuilder()
124+
.AddJsonFile("appsettings.json")
125+
.Build();
126+
127+
var logger = new LoggerConfiguration()
128+
.ReadFrom.Configuration(configuration)
129+
.CreateLogger();
27130
```
28131

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

31-
To emit JSON, rather than plain text, a formatter can be specified:
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:
32157

33158
```csharp
34-
.WriteTo.File(new JsonFormatter(), "log.txt")
159+
.WriteTo.File("log.txt",
160+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
35161
```
36162

37-
To configure an alternative formatter in XML `<appSettings>`, specify the formatter's assembly-qualified type name as the setting `value`.
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

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

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

appveyor.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2015
3+
image:
4+
- Visual Studio 2017
5+
- Ubuntu
46
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"
117
build_script:
128
- ps: ./Build.ps1
9+
for:
10+
-
11+
matrix:
12+
only:
13+
- image: Ubuntu
14+
build_script:
15+
- sh build.sh
1316
test: off
1417
artifacts:
1518
- path: artifacts/Serilog.*.nupkg
1619
deploy:
1720
- provider: NuGet
1821
api_key:
19-
secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x
22+
secure: N59tiJECUYpip6tEn0xvdmDAEiP9SIzyLEFLpwiigm/8WhJvBNs13QxzT1/3/JW/
2023
skip_symbols: true
2124
on:
2225
branch: /^(master|dev)$/

build.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
dotnet --info
5+
dotnet --list-sdks
6+
dotnet restore
7+
8+
echo "🤖 Attempting to build..."
9+
for path in src/**/*.csproj; do
10+
dotnet build -f netstandard1.3 -c Release ${path}
11+
dotnet build -f netstandard2.0 -c Release ${path}
12+
done
13+
14+
echo "🤖 Running tests..."
15+
for path in test/*.Tests/*.csproj; do
16+
dotnet test -f netcoreapp2.0 -c Release ${path}
17+
done

example/Sample/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public static void Main(string[] args)
1111
{
1212
SelfLog.Enable(Console.Out);
1313

14+
var sw = System.Diagnostics.Stopwatch.StartNew();
15+
1416
Log.Logger = new LoggerConfiguration()
1517
.WriteTo.File("log.txt")
1618
.CreateLogger();
1719

18-
var sw = System.Diagnostics.Stopwatch.StartNew();
19-
2020
for (var i = 0; i < 1000000; ++i)
2121
{
2222
Log.Information("Hello, file logger!");

0 commit comments

Comments
 (0)