-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BaseFileAppender.cs
344 lines (305 loc) · 13.7 KB
/
BaseFileAppender.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
//
// 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.
//
namespace NLog.Internal.FileAppenders
{
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using NLog.Common;
using NLog.Internal;
/// <summary>
/// Base class for optimized file appenders.
/// </summary>
[SecuritySafeCritical]
internal abstract class BaseFileAppender : IDisposable
{
#pragma warning disable S2245 // Make sure that using this pseudorandom number generator is safe here (Not security sensitive)
private readonly Random _random = new Random();
#pragma warning restore S2245 // Make sure that using this pseudorandom number generator is safe here
/// <summary>
/// Initializes a new instance of the <see cref="BaseFileAppender" /> class.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="createParameters">The create parameters.</param>
protected BaseFileAppender(string fileName, ICreateFileParameters createParameters)
{
CreateFileParameters = createParameters;
FileName = fileName;
OpenTimeUtc = DateTime.UtcNow; // to be consistent with timeToKill in FileTarget.AutoClosingTimerCallback
}
/// <summary>
/// Gets the path of the file, including file extension.
/// </summary>
/// <value>The name of the file.</value>
public string FileName { get; }
/// <summary>
/// Gets or sets the creation time for a file associated with the appender. The time returned is in Coordinated
/// Universal Time [UTC] standard.
/// </summary>
/// <returns>The creation time of the file.</returns>
public DateTime CreationTimeUtc
{
get => _creationTimeUtc;
internal set
{
_creationTimeUtc = value;
CreationTimeSource = Time.TimeSource.Current.FromSystemTime(value); // Performance optimization to skip converting every time
}
}
DateTime _creationTimeUtc;
/// <summary>
/// Gets or sets the creation time for a file associated with the appender. Synchronized by <see cref="CreationTimeUtc"/>
/// The time format is based on <see cref="NLog.Time.TimeSource" />
/// </summary>
public DateTime CreationTimeSource { get; private set; }
/// <summary>
/// Gets the last time the file associated with the appeander is opened. The time returned is in Coordinated
/// Universal Time [UTC] standard.
/// </summary>
/// <returns>The time the file was last opened.</returns>
public DateTime OpenTimeUtc { get; private set; }
/// <summary>
/// Gets the file creation parameters.
/// </summary>
/// <value>The file creation parameters.</value>
public ICreateFileParameters CreateFileParameters { get; private set; }
/// <summary>
/// Writes the specified bytes.
/// </summary>
/// <param name="bytes">The bytes.</param>
public void Write(byte[] bytes)
{
Write(bytes, 0, bytes.Length);
}
public abstract void Write(byte[] bytes, int offset, int count);
/// <summary>
/// Flushes this instance.
/// </summary>
public abstract void Flush();
/// <summary>
/// Closes this instance.
/// </summary>
public abstract void Close();
/// <summary>
/// Gets the creation time for a file associated with the appender. The time returned is in Coordinated Universal
/// Time [UTC] standard.
/// </summary>
/// <returns>The file creation time.</returns>
public abstract DateTime? GetFileCreationTimeUtc();
/// <summary>
/// Gets the length in bytes of the file associated with the appeander.
/// </summary>
/// <returns>A long value representing the length of the file in bytes.</returns>
public abstract long? GetFileLength();
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">True to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Close();
}
}
/// <summary>
/// Creates the file stream.
/// </summary>
/// <param name="allowFileSharedWriting">If set to <c>true</c> sets the file stream to allow shared writing.</param>
/// <param name="overrideBufferSize">If larger than 0 then it will be used instead of the default BufferSize for the FileStream.</param>
/// <returns>A <see cref="FileStream"/> object which can be used to write to the file.</returns>
protected FileStream CreateFileStream(bool allowFileSharedWriting, int overrideBufferSize = 0)
{
int currentDelay = CreateFileParameters.ConcurrentWriteAttemptDelay;
InternalLogger.Trace("Opening {0} with allowFileSharedWriting={1}", FileName, allowFileSharedWriting);
for (int i = 0; i < CreateFileParameters.ConcurrentWriteAttempts; ++i)
{
try
{
try
{
return TryCreateFileStream(allowFileSharedWriting, overrideBufferSize);
}
catch (DirectoryNotFoundException)
{
//we don't check the directory on beforehand, as that will really slow down writing.
if (!CreateFileParameters.CreateDirs)
{
throw;
}
var directoryName = Path.GetDirectoryName(FileName);
try
{
Directory.CreateDirectory(directoryName);
}
catch (DirectoryNotFoundException)
{
//if creating a directory failed, don't retry for this message (e.g the ConcurrentWriteAttempts below)
throw new NLogRuntimeException("Could not create directory {0}", directoryName);
}
return TryCreateFileStream(allowFileSharedWriting, overrideBufferSize);
}
}
catch (IOException)
{
if (!CreateFileParameters.ConcurrentWrites || i + 1 == CreateFileParameters.ConcurrentWriteAttempts)
{
throw; // rethrow
}
int actualDelay = _random.Next(currentDelay);
InternalLogger.Warn("Attempt #{0} to open {1} failed. Sleeping for {2}ms", i, FileName, actualDelay);
currentDelay *= 2;
AsyncHelpers.WaitForDelay(TimeSpan.FromMilliseconds(actualDelay));
}
}
throw new InvalidOperationException("Should not be reached.");
}
#if !SILVERLIGHT && !MONO && !__IOS__ && !__ANDROID__ && !NETSTANDARD
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Objects are disposed elsewhere")]
private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWriting, int overrideBufferSize)
{
int fileShare = Win32FileNativeMethods.FILE_SHARE_READ;
if (allowFileSharedWriting)
{
fileShare |= Win32FileNativeMethods.FILE_SHARE_WRITE;
}
if (CreateFileParameters.EnableFileDelete && PlatformDetector.CurrentOS != RuntimeOS.Windows)
{
fileShare |= Win32FileNativeMethods.FILE_SHARE_DELETE;
}
Microsoft.Win32.SafeHandles.SafeFileHandle handle = null;
FileStream fileStream = null;
try
{
handle = Win32FileNativeMethods.CreateFile(
fileName,
Win32FileNativeMethods.FileAccess.GenericWrite,
fileShare,
IntPtr.Zero,
Win32FileNativeMethods.CreationDisposition.OpenAlways,
CreateFileParameters.FileAttributes,
IntPtr.Zero);
if (handle.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
fileStream = new FileStream(handle, FileAccess.Write, overrideBufferSize > 0 ? overrideBufferSize : CreateFileParameters.BufferSize);
fileStream.Seek(0, SeekOrigin.End);
return fileStream;
}
catch
{
fileStream?.Dispose();
if ((handle != null) && (!handle.IsClosed))
handle.Close();
throw;
}
}
#endif
private FileStream TryCreateFileStream(bool allowFileSharedWriting, int overrideBufferSize)
{
UpdateCreationTime();
#if !SILVERLIGHT && !MONO && !__IOS__ && !__ANDROID__ && !NETSTANDARD
try
{
if (!CreateFileParameters.ForceManaged && PlatformDetector.IsWin32 && !PlatformDetector.IsMono)
{
return WindowsCreateFile(FileName, allowFileSharedWriting, overrideBufferSize);
}
}
catch (SecurityException)
{
InternalLogger.Debug("Could not use native Windows create file, falling back to managed filestream: {0}", FileName);
}
#endif
FileShare fileShare = allowFileSharedWriting ? FileShare.ReadWrite : FileShare.Read;
if (CreateFileParameters.EnableFileDelete)
{
fileShare |= FileShare.Delete;
}
return new FileStream(
FileName,
FileMode.Append,
FileAccess.Write,
fileShare,
overrideBufferSize > 0 ? overrideBufferSize : CreateFileParameters.BufferSize);
}
private void UpdateCreationTime()
{
FileInfo fileInfo = new FileInfo(FileName);
if (fileInfo.Exists)
{
CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => f.GetCreationTimeUtc(), (f) => f.GetLastWriteTimeUtc()).Value;
}
else
{
File.Create(FileName).Dispose();
CreationTimeUtc = DateTime.UtcNow;
#if !SILVERLIGHT
// Set the file's creation time to avoid being thwarted by Windows' Tunneling capabilities (https://support.microsoft.com/en-us/kb/172190).
File.SetCreationTimeUtc(FileName, CreationTimeUtc);
#endif
}
}
protected static bool MonitorForEnableFileDeleteEvent(string fileName, ref DateTime lastSimpleMonitorCheckTimeUtc)
{
long ticksDelta = DateTime.UtcNow.Ticks - lastSimpleMonitorCheckTimeUtc.Ticks;
if (ticksDelta > TimeSpan.TicksPerSecond || ticksDelta < -TimeSpan.TicksPerSecond)
{
lastSimpleMonitorCheckTimeUtc = DateTime.UtcNow;
try
{
if (!File.Exists(fileName))
{
return true;
}
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to check if File.Exists {0}", fileName);
return true;
}
}
return false;
}
}
}