Skip to content

Commit 3de698a

Browse files
authored
Merge pull request #13 from serilog/dev
2.2.0 Release
2 parents 073a5fd + 6653d4d commit 3de698a

File tree

4 files changed

+142
-9
lines changed

4 files changed

+142
-9
lines changed

Diff for: CHANGES.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.2.0-*
2+
* #11 - default to UTF-8 encoding **without BOM**
3+
4+
2.1.0
5+
* #7 - overload accepting an `ITextFormatter`
6+
17
2.0.0
2-
- Moved to new project
3-
- DotNet Core support
8+
* Moved to new project
9+
* .NET Core support

Diff for: README.md

+129-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,37 @@
22

33
Writes [Serilog](https://serilog.net) events to a set of text files, one per day.
44

5-
The filename can include the `{Date}` placeholder, which will be replaced with the date of the events contained in the file.
5+
### Getting started
6+
7+
Install the [Serilog.Sinks.RollingFile](https://nuget.org/packages/serilog.sinks.rollingfile) package from NuGet:
8+
9+
```powershell
10+
Install-Package Serilog.Sinks.RollingFile
11+
```
12+
13+
To configure the sink in C# code, call `WriteTo.RollingFile()` during logger configuration:
614

715
```csharp
816
var log = new LoggerConfiguration()
917
.WriteTo.RollingFile("log-{Date}.txt")
1018
.CreateLogger();
19+
20+
Log.Information("This will be written to the rolling file set");
21+
```
22+
23+
The filename should include the `{Date}` placeholder, which will be replaced with the date of the events contained in the file. Filenames use the `yyyyMMdd` date format so that files can be ordered using a lexicographic sort:
24+
25+
```
26+
log-20160631.txt
27+
log-20160701.txt
28+
log-20160702.txt
1129
```
1230

13-
To avoid sinking apps with runaway disk usage the rolling file sink **limits file size to 1GB by default**. The limit can be changed or removed using the `fileSizeLimitBytes` parameter.
31+
> **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](https://github.com/serilog/serilog/wiki/Provided-Sinks).
32+
33+
### Limits
34+
35+
To avoid bringing down apps with runaway disk usage the rolling file sink **limits file size to 1GB by default**. The limit can be changed or removed using the `fileSizeLimitBytes` parameter.
1436

1537
```csharp
1638
.WriteTo.RollingFile("log-{Date}.txt", fileSizeLimitBytes: null)
@@ -22,6 +44,110 @@ For the same reason, only **the most recent 31 files** are retained by default (
2244
.WriteTo.RollingFile("log-{Date}.txt", retainedFileCountLimit: null)
2345
```
2446

25-
> **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.
47+
### XML `<appSettings>` configuration
48+
49+
To use the rolling 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:
50+
51+
```powershell
52+
Install-Package Serilog.Settings.AppSettings
53+
```
54+
55+
Instead of configuring the logger in code, call `ReadFrom.AppSettings()`:
56+
57+
```csharp
58+
var log = new LoggerConfiguration()
59+
.ReadFrom.AppSettings()
60+
.CreateLogger();
61+
```
62+
63+
In your application's `App.config` or `Web.config` file, specify the rolling file sink assembly and required path format under the `<appSettings>` node:
64+
65+
```xml
66+
<configuration>
67+
<appSettings>
68+
<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
69+
<add key="serilog:write-to:RollingFile.pathFormat" value="log-{Date}.txt" />
70+
```
71+
72+
The parameters that can be set through the `serilog:write-to:RollingFile` keys are the method parameters accepted by the `WriteTo.RollingFile()` configuration method. This means, for example, that the `fileSizeLimitBytes` parameter can be set with:
73+
74+
```xml
75+
<add key="serilog:write-to:RollingFile.fileSizeLimitBytes" value="1234567" />
76+
```
77+
78+
Omitting the `value` will set the parameter to `null`:
79+
80+
```xml
81+
<add key="serilog:write-to:RollingFile.fileSizeLimitBytes" />
82+
```
83+
84+
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`:
85+
86+
```xml
87+
<add key="serilog:write-to:RollingFile.pathFormat" value="%APPDATA%\MyApp\log-{Date}.txt" />
88+
```
89+
90+
### JSON `appsettings.json` configuration
91+
92+
To use the rolling 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:
93+
94+
```powershell
95+
Install-Package Serilog.Settings.Configuration
96+
```
97+
98+
Instead of configuring the rolling file directly in code, call `ReadFrom.Configuration()`:
99+
100+
```csharp
101+
var configuration = new ConfigurationBuilder()
102+
.AddJsonFile("appsettings.json")
103+
.Build();
104+
105+
var logger = new LoggerConfiguration()
106+
.ReadFrom.Configuration(configuration)
107+
.CreateLogger();
108+
```
109+
110+
In your `appsettings.json` file, under the `Serilog` node, :
111+
112+
```json
113+
{
114+
"Serilog": {
115+
"WriteTo": [
116+
{ "Name": "RollingFile", "Args": { "pathFormat": "log-{Date}.txt" } }
117+
]
118+
}
119+
}
120+
```
121+
122+
See the XML `<appSettings>` example above for a discussion of available `Args` options.
123+
124+
### Controlling event formatting
125+
126+
The rolling file sink creates events in a fixed text format by default:
127+
128+
```
129+
2016-07-06 09:02:17.148 +10:00 [Information] HTTP "GET" "/" responded 200 in 1994 ms
130+
```
131+
132+
The format is controlled using an _output template_, which the rolling file configuration method accepts as an `outputTemplate` parameter.
133+
134+
The default format above corresponds to an output template like:
135+
136+
```csharp
137+
.WriteTo.RollingFile("log-{Date}.txt",
138+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}")
139+
```
140+
141+
##### JSON event formatting
142+
143+
To write events to the file in an alternative format such as JSON, pass an `ITextFormatter` as the first argument:
144+
145+
```csharp
146+
.WriteTo.RollingFile(new JsonFormatter(), "log-{Date}.txt")
147+
```
148+
149+
### Alternatives
150+
151+
The default rolling file sink is designed to suit most applications. So that we can keep it maintainable and reliable, it does not provide a large range of optional behavior. Check out alternative implemementations like [this one](https://github.com/BedeGaming/sinks-rollingfile) if your needs aren't met by the default version.
26152

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

Diff for: src/Serilog.Sinks.RollingFile/Sinks/RollingFile/RollingFileSink.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public RollingFileSink(string pathFormat,
7474
_textFormatter = textFormatter;
7575
_fileSizeLimitBytes = fileSizeLimitBytes;
7676
_retainedFileCountLimit = retainedFileCountLimit;
77-
_encoding = encoding ?? Encoding.UTF8;
77+
_encoding = encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
7878
_buffered = buffered;
7979
}
8080

Diff for: src/Serilog.Sinks.RollingFile/project.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.1.0-*",
2+
"version": "2.2.0-*",
33
"description": "The rolling file sink for Serilog - Simple .NET logging with fully-structured events",
44
"authors": [ "Serilog Contributors" ],
55
"packOptions": {
@@ -23,8 +23,9 @@
2323
"dependencies": {
2424
"System.IO": "4.1.0",
2525
"System.IO.FileSystem.Primitives": "4.0.1",
26-
"System.Runtime.InteropServices": "4.1.0"
26+
"System.Runtime.InteropServices": "4.1.0",
27+
"System.Text.Encoding.Extensions": "4.0.11"
2728
}
2829
}
2930
}
30-
}
31+
}

0 commit comments

Comments
 (0)