-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
FileAppenderCache.cs
580 lines (515 loc) · 21.5 KB
/
FileAppenderCache.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//
// Copyright (c) 2004-2019 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
#if !SILVERLIGHT && !__ANDROID__ && !__IOS__ && !NETSTANDARD1_3
// Unfortunately, Xamarin Android and Xamarin iOS don't support mutexes (see https://github.com/mono/mono/blob/3a9e18e5405b5772be88bfc45739d6a350560111/mcs/class/corlib/System.Threading/Mutex.cs#L167) so the BaseFileAppender class now throws an exception in the constructor.
#define SupportsMutex
#endif
namespace NLog.Internal.FileAppenders
{
using System;
using System.IO;
using System.Threading;
using NLog.Common;
/// <summary>
/// Maintains a collection of file appenders usually associated with file targets.
/// </summary>
internal sealed class FileAppenderCache : IDisposable
{
private readonly BaseFileAppender[] _appenders;
private Timer _autoClosingTimer;
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
private string _archiveFilePatternToWatch;
private readonly MultiFileWatcher _externalFileArchivingWatcher = new MultiFileWatcher(NotifyFilters.DirectoryName | NotifyFilters.FileName);
private bool _logFileWasArchived;
#endif
/// <summary>
/// An "empty" instance of the <see cref="FileAppenderCache"/> class with zero size and empty list of appenders.
/// </summary>
public static readonly FileAppenderCache Empty = new FileAppenderCache();
/// <summary>
/// Initializes a new "empty" instance of the <see cref="FileAppenderCache"/> class with zero size and empty
/// list of appenders.
/// </summary>
private FileAppenderCache() : this(0, null, null) { }
/// <summary>
/// Initializes a new instance of the <see cref="FileAppenderCache"/> class.
/// </summary>
/// <remarks>
/// The size of the list should be positive. No validations are performed during initialisation as it is an
/// intenal class.
/// </remarks>
/// <param name="size">Total number of appenders allowed in list.</param>
/// <param name="appenderFactory">Factory used to create each appender.</param>
/// <param name="createFileParams">Parameters used for creating a file.</param>
public FileAppenderCache(int size, IFileAppenderFactory appenderFactory, ICreateFileParameters createFileParams)
{
Size = size;
Factory = appenderFactory;
CreateFileParameters = createFileParams;
_appenders = new BaseFileAppender[Size];
_autoClosingTimer = new Timer(AutoClosingTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
_externalFileArchivingWatcher.FileChanged += ExternalFileArchivingWatcher_OnFileChanged;
#endif
}
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
private void ExternalFileArchivingWatcher_OnFileChanged(object sender, FileSystemEventArgs e)
{
if (_logFileWasArchived || CheckCloseAppenders == null || _autoClosingTimer == null)
{
return;
}
if (FileAppenderFolderChanged(e.FullPath))
{
if ((e.ChangeType & (WatcherChangeTypes.Deleted | WatcherChangeTypes.Renamed)) != 0)
_logFileWasArchived = true; // File Appender file deleted/renamed
}
else
{
if ((e.ChangeType & WatcherChangeTypes.Created) == WatcherChangeTypes.Created)
_logFileWasArchived = true; // Something was created in the archive folder
}
if (_logFileWasArchived && _autoClosingTimer != null)
{
_autoClosingTimer.Change(50, Timeout.Infinite);
}
}
private bool FileAppenderFolderChanged(string fullPath)
{
if (!string.IsNullOrEmpty(fullPath))
{
if (string.IsNullOrEmpty(_archiveFilePatternToWatch))
{
return true;
}
else
{
string archiveFolderPath = Path.GetDirectoryName(_archiveFilePatternToWatch);
if (!string.IsNullOrEmpty(archiveFolderPath))
{
string currentFolderPath = Path.GetDirectoryName(fullPath);
return !string.Equals(archiveFolderPath, currentFolderPath, StringComparison.OrdinalIgnoreCase);
}
else
{
return true;
}
}
}
return false;
}
/// <summary>
/// The archive file path pattern that is used to detect when archiving occurs.
/// </summary>
public string ArchiveFilePatternToWatch
{
get => _archiveFilePatternToWatch;
set
{
if (_archiveFilePatternToWatch != value)
{
if (!string.IsNullOrEmpty(_archiveFilePatternToWatch))
{
string directoryPath = Path.GetDirectoryName(_archiveFilePatternToWatch);
if (string.IsNullOrEmpty(directoryPath))
_externalFileArchivingWatcher.StopWatching(directoryPath);
}
_archiveFilePatternToWatch = value;
_logFileWasArchived = false;
}
}
}
/// <summary>
/// Invalidates appenders for all files that were archived.
/// </summary>
public void InvalidateAppendersForArchivedFiles()
{
if (_logFileWasArchived)
{
_logFileWasArchived = false;
InternalLogger.Trace("FileAppender: Invalidate archived files");
CloseAppenders("Cleanup Archive");
}
}
#endif
private void AutoClosingTimerCallback(object state)
{
var checkCloseAppenders = CheckCloseAppenders;
if (checkCloseAppenders != null)
{
checkCloseAppenders(this, EventArgs.Empty);
}
}
/// <summary>
/// Gets the parameters which will be used for creating a file.
/// </summary>
public ICreateFileParameters CreateFileParameters { get; private set; }
/// <summary>
/// Gets the file appender factory used by all the appenders in this list.
/// </summary>
public IFileAppenderFactory Factory { get; private set; }
/// <summary>
/// Gets the number of appenders which the list can hold.
/// </summary>
public int Size { get; private set; }
/// <summary>
/// Subscribe to background monitoring of active file appenders
/// </summary>
public event EventHandler CheckCloseAppenders;
/// <summary>
/// It allocates the first slot in the list when the file name does not already in the list and clean up any
/// unused slots.
/// </summary>
/// <param name="fileName">File name associated with a single appender.</param>
/// <returns>The allocated appender.</returns>
/// <exception cref="NullReferenceException">
/// Thrown when <see cref="M:AllocateAppender"/> is called on an <c>Empty</c><see cref="FileAppenderCache"/> instance.
/// </exception>
public BaseFileAppender AllocateAppender(string fileName)
{
//
// BaseFileAppender.Write is the most expensive operation here
// so the in-memory data structure doesn't have to be
// very sophisticated. It's a table-based LRU, where we move
// the used element to become the first one.
// The number of items is usually very limited so the
// performance should be equivalent to the one of the hashtable.
//
BaseFileAppender appenderToWrite = null;
int freeSpot = _appenders.Length - 1;
for (int i = 0; i < _appenders.Length; ++i)
{
// Use empty slot in recent appender list, if there is one.
if (_appenders[i] == null)
{
freeSpot = i;
break;
}
if (string.Equals(_appenders[i].FileName, fileName, StringComparison.OrdinalIgnoreCase))
{
// found it, move it to the first place on the list
// (MRU)
BaseFileAppender app = _appenders[i];
if (i > 0)
{
// file open has a chance of failure
// if it fails in the constructor, we won't modify any data structures
for (int j = i; j > 0; --j)
{
_appenders[j] = _appenders[j - 1];
}
_appenders[0] = app;
}
appenderToWrite = app;
break;
}
}
if (appenderToWrite == null)
{
appenderToWrite = CreateAppender(fileName, freeSpot);
}
return appenderToWrite;
}
private BaseFileAppender CreateAppender(string fileName, int freeSpot)
{
BaseFileAppender appenderToWrite;
try
{
InternalLogger.Debug("Creating file appender: {0}", fileName);
BaseFileAppender newAppender = Factory.Open(fileName, CreateFileParameters);
if (_appenders[freeSpot] != null)
{
CloseAppender(_appenders[freeSpot], "Stale", false);
_appenders[freeSpot] = null;
}
for (int j = freeSpot; j > 0; --j)
{
_appenders[j] = _appenders[j - 1];
}
_appenders[0] = newAppender;
appenderToWrite = newAppender;
if (CheckCloseAppenders != null)
{
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
if (freeSpot == 0)
_logFileWasArchived = false;
if (!string.IsNullOrEmpty(_archiveFilePatternToWatch))
{
string directoryPath = Path.GetDirectoryName(_archiveFilePatternToWatch);
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
_externalFileArchivingWatcher.Watch(_archiveFilePatternToWatch); // Always monitor the archive-folder
}
_externalFileArchivingWatcher.Watch(appenderToWrite.FileName); // Monitor the active file-appender
#endif
}
}
catch (Exception ex)
{
InternalLogger.Warn(ex, "Failed to create file appender: {0}", fileName);
throw;
}
return appenderToWrite;
}
/// <summary>
/// Close all the allocated appenders.
/// </summary>
public void CloseAppenders(string reason)
{
for (int i = 0; i < _appenders.Length; ++i)
{
var oldAppender = _appenders[i];
if (oldAppender == null)
{
break;
}
CloseAppender(oldAppender, reason, true);
_appenders[i] = null;
oldAppender.Dispose(); // Dispose of Archive Mutex
}
}
/// <summary>
/// Close the allocated appenders initialized before the supplied time.
/// </summary>
/// <param name="expireTime">The time which prior the appenders considered expired</param>
public void CloseAppenders(DateTime expireTime)
{
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
if (_logFileWasArchived)
{
_logFileWasArchived = false;
CloseAppenders("Cleanup Timer");
}
else
#endif
{
if (expireTime != DateTime.MinValue)
{
for (int i = 0; i < _appenders.Length; ++i)
{
if (_appenders[i] == null)
{
break;
}
if (_appenders[i].OpenTimeUtc < expireTime)
{
for (int j = i; j < _appenders.Length; ++j)
{
var oldAppender = _appenders[j];
if (oldAppender == null)
{
break;
}
CloseAppender(oldAppender, "Expired", i == 0);
_appenders[j] = null;
oldAppender.Dispose(); // Dispose of Archive Mutex
}
break;
}
}
}
}
}
/// <summary>
/// Fluch all the allocated appenders.
/// </summary>
public void FlushAppenders()
{
foreach (BaseFileAppender appender in _appenders)
{
if (appender == null)
{
break;
}
try
{
appender.Flush();
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to flush file '{0}'.", appender.FileName);
InvalidateAppender(appender.FileName)?.Dispose();
throw;
}
}
}
private BaseFileAppender GetAppender(string fileName)
{
for (int i = 0; i < _appenders.Length; ++i)
{
BaseFileAppender appender = _appenders[i];
if (appender == null)
break;
if (string.Equals(appender.FileName, fileName, StringComparison.OrdinalIgnoreCase))
return appender;
}
return null;
}
public DateTime? GetFileCreationTimeSource(string filePath, DateTime? fallbackTimeSource = null)
{
var appender = GetAppender(filePath);
DateTime? result = null;
if (appender != null)
{
try
{
result = FileCharacteristicsHelper.ValidateFileCreationTime(appender, (f) => f.GetFileCreationTimeUtc(), f => fallbackTimeSource);
if (result.HasValue)
{
// Check if cached value is still valid, and update if not (Will automatically update CreationTimeSource)
DateTime cachedTimeUtc = appender.CreationTimeUtc;
if (result.Value != cachedTimeUtc)
{
appender.CreationTimeUtc = result.Value;
}
return appender.CreationTimeSource;
}
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to get file creation time for file '{0}'.", appender.FileName);
InvalidateAppender(appender.FileName)?.Dispose();
throw;
}
}
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
result = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => fallbackTimeSource, (f) => f.GetLastWriteTimeUtc()).Value;
return Time.TimeSource.Current.FromSystemTime(result.Value);
}
return result;
}
/// <summary>
/// File Archive Logic uses the File-Creation-TimeStamp to detect if time to archive, and the File-LastWrite-Timestamp to name the archive-file.
/// </summary>
/// <remarks>
/// NLog always closes all relevant appenders during archive operation, so no need to lookup file-appender
/// </remarks>
public DateTime? GetFileLastWriteTimeUtc(string filePath)
{
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
return fileInfo.GetLastWriteTimeUtc();
}
return null;
}
public long? GetFileLength(string filePath)
{
var appender = GetAppender(filePath);
if (appender != null)
{
try
{
var result = appender.GetFileLength();
if (result.HasValue)
{
return result;
}
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to get length for file '{0}'.", appender.FileName);
InvalidateAppender(appender.FileName)?.Dispose();
throw;
}
}
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
return fileInfo.Length;
}
return null;
}
/// <summary>
/// Closes the specified appender and removes it from the list.
/// </summary>
/// <param name="filePath">File name of the appender to be closed.</param>
/// <returns>File Appender that matched the filePath (null if none found)</returns>
public BaseFileAppender InvalidateAppender(string filePath)
{
for (int i = 0; i < _appenders.Length; ++i)
{
var oldAppender = _appenders[i];
if (oldAppender == null)
{
break;
}
if (string.Equals(oldAppender.FileName, filePath, StringComparison.OrdinalIgnoreCase))
{
for (int j = i; j < _appenders.Length - 1; ++j)
{
_appenders[j] = _appenders[j + 1];
}
_appenders[_appenders.Length - 1] = null;
CloseAppender(oldAppender, "Invalidate", _appenders[0] == null);
return oldAppender; // Return without Dispose of Archive Mutex
}
}
return null;
}
private void CloseAppender(BaseFileAppender appender, string reason, bool lastAppender)
{
InternalLogger.Debug("FileAppender Closing {0} - {1}", reason, appender.FileName);
if (lastAppender)
{
// No active appenders, deactivate background tasks
_autoClosingTimer.Change(Timeout.Infinite, Timeout.Infinite);
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
_externalFileArchivingWatcher.StopWatching();
_logFileWasArchived = false;
}
else
{
_externalFileArchivingWatcher.StopWatching(appender.FileName);
#endif
}
appender.Close();
}
public void Dispose()
{
CheckCloseAppenders = null;
#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD1_3
_externalFileArchivingWatcher.Dispose();
_logFileWasArchived = false;
#endif
var currentTimer = _autoClosingTimer;
if (currentTimer != null)
{
_autoClosingTimer = null;
currentTimer.WaitForDispose(TimeSpan.Zero);
}
}
}
}