@@ -39,11 +39,12 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable
3939 readonly int ? _retainedFileCountLimit ;
4040 readonly Encoding _encoding ;
4141 readonly bool _buffered ;
42+ readonly bool _shared ;
4243 readonly object _syncRoot = new object ( ) ;
4344
4445 bool _isDisposed ;
4546 DateTime ? _nextCheckpoint ;
46- FileSink _currentFile ;
47+ ILogEventSink _currentFile ;
4748
4849 /// <summary>Construct a <see cref="RollingFileSink"/>.</summary>
4950 /// <param name="pathFormat">String describing the location of the log files,
@@ -54,28 +55,36 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable
5455 /// For unrestricted growth, pass null. The default is 1 GB.</param>
5556 /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained,
5657 /// 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>
5859 /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
5960 /// is false.</param>
61+ /// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param>
6062 /// <returns>Configuration object allowing method chaining.</returns>
6163 /// <remarks>The file will be written using the UTF-8 character set.</remarks>
6264 public RollingFileSink ( string pathFormat ,
6365 ITextFormatter textFormatter ,
6466 long ? fileSizeLimitBytes ,
6567 int ? retainedFileCountLimit ,
6668 Encoding encoding = null ,
67- bool buffered = false )
69+ bool buffered = false ,
70+ bool shared = false )
6871 {
6972 if ( pathFormat == null ) throw new ArgumentNullException ( nameof ( pathFormat ) ) ;
7073 if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative" ) ;
7174 if ( retainedFileCountLimit . HasValue && retainedFileCountLimit < 1 ) throw new ArgumentException ( "Zero or negative value provided; retained file count limit must be at least 1" ) ;
7275
76+ #if ! SHARING
77+ if ( shared )
78+ throw new NotSupportedException ( "File sharing is not supported on this platform." ) ;
79+ #endif
80+
7381 _roller = new TemplatedPathRoller ( pathFormat ) ;
7482 _textFormatter = textFormatter ;
7583 _fileSizeLimitBytes = fileSizeLimitBytes ;
7684 _retainedFileCountLimit = retainedFileCountLimit ;
77- _encoding = encoding ?? new UTF8Encoding ( encoderShouldEmitUTF8Identifier : false ) ;
85+ _encoding = encoding ;
7886 _buffered = buffered ;
87+ _shared = shared ;
7988 }
8089
8190 /// <summary>
@@ -98,8 +107,7 @@ public void Emit(LogEvent logEvent)
98107 // If the file was unable to be opened on the last attempt, it will remain
99108 // null until the next checkpoint passes, at which time another attempt will be made to
100109 // open it.
101- if ( _currentFile != null )
102- _currentFile . Emit ( logEvent ) ;
110+ _currentFile ? . Emit ( logEvent ) ;
103111 }
104112 }
105113
@@ -148,7 +156,13 @@ void OpenFile(DateTime now)
148156
149157 try
150158 {
159+ #if SHARING
160+ _currentFile = _shared ?
161+ ( ILogEventSink ) new SharedFileSink ( path , _textFormatter , _fileSizeLimitBytes , _encoding ) :
162+ new FileSink ( path , _textFormatter , _fileSizeLimitBytes , _encoding , _buffered ) ;
163+ #else
151164 _currentFile = new FileSink ( path , _textFormatter , _fileSizeLimitBytes , _encoding , _buffered ) ;
165+ #endif
152166 }
153167 catch ( IOException ex )
154168 {
@@ -223,7 +237,7 @@ void CloseFile()
223237 {
224238 if ( _currentFile != null )
225239 {
226- _currentFile . Dispose ( ) ;
240+ ( _currentFile as IDisposable ) ? . Dispose ( ) ;
227241 _currentFile = null ;
228242 }
229243
0 commit comments