-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathRollingFileSink.cs
244 lines (215 loc) · 9.65 KB
/
RollingFileSink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2013-2016 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.IO;
using System.Linq;
using System.Text;
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.File;
namespace Serilog.Sinks.RollingFile
{
/// <summary>
/// Write log events to a series of files. Each file will be named according to
/// the date of the first log entry written to it. Only simple date-based rolling is
/// currently supported.
/// </summary>
public sealed class RollingFileSink : ILogEventSink, IFlushableFileSink, IDisposable
{
readonly TemplatedPathRoller _roller;
readonly ITextFormatter _textFormatter;
readonly long? _fileSizeLimitBytes;
readonly int? _retainedFileCountLimit;
readonly Encoding _encoding;
readonly bool _buffered;
readonly bool _shared;
readonly object _syncRoot = new object();
bool _isDisposed;
DateTime? _nextCheckpoint;
ILogEventSink _currentFile;
/// <summary>Construct a <see cref="RollingFileSink"/>.</summary>
/// <param name="pathFormat">String describing the location of the log files,
/// with {Date} in the place of the file date. E.g. "Logs\myapp-{Date}.log" will result in log
/// files such as "Logs\myapp-2013-10-20.log", "Logs\myapp-2013-10-21.log" and so on.</param>
/// <param name="textFormatter">Formatter used to convert log events to text.</param>
/// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which a log file will be allowed to grow.
/// For unrestricted growth, pass null. The default is 1 GB.</param>
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
/// is false.</param>
/// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
public RollingFileSink(string pathFormat,
ITextFormatter textFormatter,
long? fileSizeLimitBytes,
int? retainedFileCountLimit,
Encoding encoding = null,
bool buffered = false,
bool shared = false)
{
if (pathFormat == null) throw new ArgumentNullException(nameof(pathFormat));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
_roller = new TemplatedPathRoller(pathFormat);
_textFormatter = textFormatter;
_fileSizeLimitBytes = fileSizeLimitBytes;
_retainedFileCountLimit = retainedFileCountLimit;
_encoding = encoding;
_buffered = buffered;
_shared = shared;
}
/// <summary>
/// Emit the provided log event to the sink.
/// </summary>
/// <param name="logEvent">The log event to write.</param>
/// <remarks>Events that come in out-of-order (e.g. around the rollovers)
/// may end up written to a later file than their timestamp
/// would indicate.</remarks>
public void Emit(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
lock (_syncRoot)
{
if (_isDisposed) throw new ObjectDisposedException("The rolling log file has been disposed.");
AlignCurrentFileTo(Clock.DateTimeNow);
// If the file was unable to be opened on the last attempt, it will remain
// null until the next checkpoint passes, at which time another attempt will be made to
// open it.
_currentFile?.Emit(logEvent);
}
}
void AlignCurrentFileTo(DateTime now)
{
if (!_nextCheckpoint.HasValue)
{
OpenFile(now);
}
else if (now >= _nextCheckpoint.Value)
{
CloseFile();
OpenFile(now);
}
}
void OpenFile(DateTime now)
{
var currentCheckpoint = _roller.GetCurrentCheckpoint(now);
// We only take one attempt at it because repeated failures
// to open log files REALLY slow an app down.
_nextCheckpoint = _roller.GetNextCheckpoint(now);
var existingFiles = Enumerable.Empty<string>();
try
{
existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
.Select(Path.GetFileName);
}
catch (DirectoryNotFoundException) { }
var latestForThisCheckpoint = _roller
.SelectMatches(existingFiles)
.Where(m => m.DateTime == currentCheckpoint)
.OrderByDescending(m => m.SequenceNumber)
.FirstOrDefault();
var sequence = latestForThisCheckpoint != null ? latestForThisCheckpoint.SequenceNumber : 0;
const int maxAttempts = 3;
for (var attempt = 0; attempt < maxAttempts; attempt++)
{
string path;
_roller.GetLogFilePath(now, sequence, out path);
try
{
_currentFile = _shared ?
(ILogEventSink)new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) :
new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered);
}
catch (IOException ex)
{
if (IOErrors.IsLockedFile(ex))
{
SelfLog.WriteLine("Rolling file target {0} was locked, attempting to open next in sequence (attempt {1})", path, attempt + 1);
sequence++;
continue;
}
throw;
}
ApplyRetentionPolicy(path);
return;
}
}
void ApplyRetentionPolicy(string currentFilePath)
{
if (_retainedFileCountLimit == null) return;
var currentFileName = Path.GetFileName(currentFilePath);
// We consider the current file to exist, even if nothing's been written yet,
// because files are only opened on response to an event being processed.
var potentialMatches = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
.Select(Path.GetFileName)
.Union(new [] { currentFileName });
var newestFirst = _roller
.SelectMatches(potentialMatches)
.OrderByDescending(m => m.DateTime)
.ThenByDescending(m => m.SequenceNumber)
.Select(m => m.Filename);
var toRemove = newestFirst
.Where(n => StringComparer.OrdinalIgnoreCase.Compare(currentFileName, n) != 0)
.Skip(_retainedFileCountLimit.Value - 1)
.ToList();
foreach (var obsolete in toRemove)
{
var fullPath = Path.Combine(_roller.LogFileDirectory, obsolete);
try
{
System.IO.File.Delete(fullPath);
}
catch (Exception ex)
{
SelfLog.WriteLine("Error {0} while removing obsolete file {1}", ex, fullPath);
}
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or
/// resetting unmanaged resources.
/// </summary>
public void Dispose()
{
lock (_syncRoot)
{
if (_currentFile == null) return;
CloseFile();
_isDisposed = true;
}
}
void CloseFile()
{
if (_currentFile != null)
{
(_currentFile as IDisposable)?.Dispose();
_currentFile = null;
}
_nextCheckpoint = null;
}
/// <inheritdoc />
public void FlushToDisk()
{
lock (_syncRoot)
{
(_currentFile as IFlushableFileSink)?.FlushToDisk();
}
}
}
}