@@ -47,6 +47,7 @@ public static class FileLoggerConfigurationExtensions
4747 /// will be written in full even if it exceeds the limit.</param>
4848 /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
4949 /// is false.</param>
50+ /// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
5051 /// <returns>Configuration object allowing method chaining.</returns>
5152 /// <remarks>The file will be written using the UTF-8 character set.</remarks>
5253 public static LoggerConfiguration File (
@@ -57,14 +58,15 @@ public static LoggerConfiguration File(
5758 IFormatProvider formatProvider = null ,
5859 long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
5960 LoggingLevelSwitch levelSwitch = null ,
60- bool buffered = false )
61+ bool buffered = false ,
62+ bool shared = false )
6163 {
6264 if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
6365 if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
6466 if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
6567
6668 var formatter = new MessageTemplateTextFormatter ( outputTemplate , formatProvider ) ;
67- return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered ) ;
69+ return File ( sinkConfiguration , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared ) ;
6870 }
6971
7072 /// <summary>
@@ -73,7 +75,7 @@ public static LoggerConfiguration File(
7375 /// <param name="sinkConfiguration">Logger sink configuration.</param>
7476 /// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
7577 /// text for the file. If control of regular text formatting is required, use the other
76- /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool)"/>
78+ /// overload of <see cref="File(LoggerSinkConfiguration, string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool, bool )"/>
7779 /// and specify the outputTemplate parameter instead.
7880 /// </param>
7981 /// <param name="path">Path to the file.</param>
@@ -86,6 +88,7 @@ public static LoggerConfiguration File(
8688 /// will be written in full even if it exceeds the limit.</param>
8789 /// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
8890 /// is false.</param>
91+ /// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
8992 /// <returns>Configuration object allowing method chaining.</returns>
9093 /// <remarks>The file will be written using the UTF-8 character set.</remarks>
9194 public static LoggerConfiguration File (
@@ -95,9 +98,10 @@ public static LoggerConfiguration File(
9598 LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
9699 long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
97100 LoggingLevelSwitch levelSwitch = null ,
98- bool buffered = false )
101+ bool buffered = false ,
102+ bool shared = false )
99103 {
100- return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered ) ;
104+ return ConfigureFile ( sinkConfiguration . Sink , formatter , path , restrictedToMinimumLevel , fileSizeLimitBytes , levelSwitch , buffered : buffered , shared : shared ) ;
101105 }
102106
103107 /// <summary>
@@ -136,7 +140,7 @@ public static LoggerConfiguration File(
136140 /// <param name="sinkConfiguration">Logger sink configuration.</param>
137141 /// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
138142 /// text for the file. If control of regular text formatting is required, use the other
139- /// overload of <see cref="File(LoggerSinkConfiguration , string, LogEventLevel, string, IFormatProvider, long?, LoggingLevelSwitch, bool )"/>
143+ /// overload of <see cref="File(LoggerAuditSinkConfiguration , string, LogEventLevel, string, IFormatProvider, LoggingLevelSwitch)"/>
140144 /// and specify the outputTemplate parameter instead.
141145 /// </param>
142146 /// <param name="path">Path to the file.</param>
@@ -164,17 +168,39 @@ static LoggerConfiguration ConfigureFile(
164168 long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
165169 LoggingLevelSwitch levelSwitch = null ,
166170 bool buffered = false ,
167- bool propagateExceptions = false )
171+ bool propagateExceptions = false ,
172+ bool shared = false )
168173 {
169174 if ( addSink == null ) throw new ArgumentNullException ( nameof ( addSink ) ) ;
170175 if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
171176 if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
172177 if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative" ) ;
173178
174- FileSink sink ;
179+ if ( shared )
180+ {
181+ #if ! ATOMIC_APPEND
182+ throw new NotSupportedException ( "File sharing is not supported on this platform." ) ;
183+ #endif
184+
185+ if ( buffered )
186+ throw new ArgumentException ( "Buffered writes are not available when file sharing is enabled." , nameof ( buffered ) ) ;
187+ }
188+
189+ ILogEventSink sink ;
175190 try
176191 {
177- sink = new FileSink ( path , formatter , fileSizeLimitBytes , buffered : buffered ) ;
192+ #if ATOMIC_APPEND
193+ if ( shared )
194+ {
195+ sink = new SharedFileSink ( path , formatter , fileSizeLimitBytes ) ;
196+ }
197+ else
198+ {
199+ #endif
200+ sink = new FileSink ( path , formatter , fileSizeLimitBytes , buffered : buffered ) ;
201+ #if ATOMIC_APPEND
202+ }
203+ #endif
178204 }
179205 catch ( Exception ex )
180206 {
0 commit comments