@@ -39,11 +39,12 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable
39
39
readonly int ? _retainedFileCountLimit ;
40
40
readonly Encoding _encoding ;
41
41
readonly bool _buffered ;
42
+ readonly bool _shared ;
42
43
readonly object _syncRoot = new object ( ) ;
43
44
44
45
bool _isDisposed ;
45
46
DateTime ? _nextCheckpoint ;
46
- FileSink _currentFile ;
47
+ ILogEventSink _currentFile ;
47
48
48
49
/// <summary>Construct a <see cref="RollingFileSink"/>.</summary>
49
50
/// <param name="pathFormat">String describing the location of the log files,
@@ -54,28 +55,36 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable
54
55
/// For unrestricted growth, pass null. The default is 1 GB.</param>
55
56
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
56
57
/// including the current log file. For unlimited retention, pass null. The default is 31.</param>
57
- /// <param name="encoding">Character encoding used to write the text file. The default is UTF-8.</param>
58
+ /// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM .</param>
58
59
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
59
60
/// is false.</param>
61
+ /// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
60
62
/// <returns>Configuration object allowing method chaining.</returns>
61
63
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
62
64
public RollingFileSink ( string pathFormat ,
63
65
ITextFormatter textFormatter ,
64
66
long ? fileSizeLimitBytes ,
65
67
int ? retainedFileCountLimit ,
66
68
Encoding encoding = null ,
67
- bool buffered = false )
69
+ bool buffered = false ,
70
+ bool shared = false )
68
71
{
69
72
if ( pathFormat == null ) throw new ArgumentNullException ( nameof ( pathFormat ) ) ;
70
73
if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative" ) ;
71
74
if ( retainedFileCountLimit . HasValue && retainedFileCountLimit < 1 ) throw new ArgumentException ( "Zero or negative value provided; retained file count limit must be at least 1" ) ;
72
75
76
+ #if ! SHARING
77
+ if ( shared )
78
+ throw new NotSupportedException ( "File sharing is not supported on this platform." ) ;
79
+ #endif
80
+
73
81
_roller = new TemplatedPathRoller ( pathFormat ) ;
74
82
_textFormatter = textFormatter ;
75
83
_fileSizeLimitBytes = fileSizeLimitBytes ;
76
84
_retainedFileCountLimit = retainedFileCountLimit ;
77
- _encoding = encoding ?? new UTF8Encoding ( encoderShouldEmitUTF8Identifier : false ) ;
85
+ _encoding = encoding ;
78
86
_buffered = buffered ;
87
+ _shared = shared ;
79
88
}
80
89
81
90
/// <summary>
@@ -98,8 +107,7 @@ public void Emit(LogEvent logEvent)
98
107
// If the file was unable to be opened on the last attempt, it will remain
99
108
// null until the next checkpoint passes, at which time another attempt will be made to
100
109
// open it.
101
- if ( _currentFile != null )
102
- _currentFile . Emit ( logEvent ) ;
110
+ _currentFile ? . Emit ( logEvent ) ;
103
111
}
104
112
}
105
113
@@ -148,7 +156,13 @@ void OpenFile(DateTime now)
148
156
149
157
try
150
158
{
159
+ #if SHARING
160
+ _currentFile = _shared ?
161
+ ( ILogEventSink ) new SharedFileSink ( path , _textFormatter , _fileSizeLimitBytes , _encoding ) :
162
+ new FileSink ( path , _textFormatter , _fileSizeLimitBytes , _encoding , _buffered ) ;
163
+ #else
151
164
_currentFile = new FileSink ( path , _textFormatter , _fileSizeLimitBytes , _encoding , _buffered ) ;
165
+ #endif
152
166
}
153
167
catch ( IOException ex )
154
168
{
@@ -223,7 +237,7 @@ void CloseFile()
223
237
{
224
238
if ( _currentFile != null )
225
239
{
226
- _currentFile . Dispose ( ) ;
240
+ ( _currentFile as IDisposable ) ? . Dispose ( ) ;
227
241
_currentFile = null ;
228
242
}
229
243
0 commit comments