Skip to content

Commit 6ad69c1

Browse files
committed
Bring the README quality into line with the rolling file sink's
1 parent 483e4e8 commit 6ad69c1

File tree

1 file changed

+153
-13
lines changed

1 file changed

+153
-13
lines changed

README.md

Lines changed: 153 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,189 @@
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 -Pre
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" />
27108
```
28109

29-
### JSON formatting
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+
```
30117

31-
To emit JSON, rather than plain text, a formatter can be specified:
118+
Instead of configuring the file directly in code, call `ReadFrom.Configuration()`:
32119

33120
```csharp
34-
.WriteTo.File(new JsonFormatter(), "log.txt")
121+
var configuration = new ConfigurationBuilder()
122+
.AddJsonFile("appsettings.json")
123+
.Build();
124+
125+
var logger = new LoggerConfiguration()
126+
.ReadFrom.Configuration(configuration)
127+
.CreateLogger();
128+
```
129+
130+
In your `appsettings.json` file, under the `Serilog` node, :
131+
132+
```json
133+
{
134+
"Serilog": {
135+
"WriteTo": [
136+
{ "Name": "File", "Args": { "path": "log.txt", "rollingInterval": "Day" } }
137+
]
138+
}
139+
}
35140
```
36141

37-
To configure an alternative formatter in XML `<appSettings>`, specify the formatter's assembly-qualified type name as the setting `value`.
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:
155+
156+
```csharp
157+
.WriteTo.File("log.txt",
158+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{u3}] {Message:lj}{NewLine}{Exception}")
159+
```
160+
161+
##### JSON event formatting
162+
163+
To write events to the file in an alternative format such as JSON, pass an `ITextFormatter` as the first argument:
164+
165+
```csharp
166+
.WriteTo.File(new JsonFormatter(), "log.txt")
167+
```
38168

39169
### Shared log files
40170

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

43173
```csharp
44174
.WriteTo.File("log.txt", shared: true)
45175
```
46176

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

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

0 commit comments

Comments
 (0)