@@ -47,6 +47,7 @@ public static class FileLoggerConfigurationExtensions
47
47
/// will be written in full even if it exceeds the limit.</param>
48
48
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
49
49
/// is false.</param>
50
+ /// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
50
51
/// <returns>Configuration object allowing method chaining.</returns>
51
52
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
52
53
public static LoggerConfiguration File (
@@ -57,14 +58,15 @@ public static LoggerConfiguration File(
57
58
IFormatProvider formatProvider = null ,
58
59
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
59
60
LoggingLevelSwitch levelSwitch = null ,
60
- bool buffered = false )
61
+ bool buffered = false ,
62
+ bool shared = false )
61
63
{
62
64
if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
63
65
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
64
66
if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
65
67
66
68
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 ) ;
68
70
}
69
71
70
72
/// <summary>
@@ -73,7 +75,7 @@ public static LoggerConfiguration File(
73
75
/// <param name="sinkConfiguration">Logger sink configuration.</param>
74
76
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
75
77
/// 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 )"/>
77
79
/// and specify the outputTemplate parameter instead.
78
80
/// </param>
79
81
/// <param name="path">Path to the file.</param>
@@ -86,6 +88,7 @@ public static LoggerConfiguration File(
86
88
/// will be written in full even if it exceeds the limit.</param>
87
89
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default
88
90
/// is false.</param>
91
+ /// <param name="shared">Allow the log file to be shared by multiple processes. The default is false.</param>
89
92
/// <returns>Configuration object allowing method chaining.</returns>
90
93
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
91
94
public static LoggerConfiguration File (
@@ -95,9 +98,10 @@ public static LoggerConfiguration File(
95
98
LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
96
99
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
97
100
LoggingLevelSwitch levelSwitch = null ,
98
- bool buffered = false )
101
+ bool buffered = false ,
102
+ bool shared = false )
99
103
{
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 ) ;
101
105
}
102
106
103
107
/// <summary>
@@ -136,7 +140,7 @@ public static LoggerConfiguration File(
136
140
/// <param name="sinkConfiguration">Logger sink configuration.</param>
137
141
/// <param name="formatter">A formatter, such as <see cref="JsonFormatter"/>, to convert the log events into
138
142
/// 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)"/>
140
144
/// and specify the outputTemplate parameter instead.
141
145
/// </param>
142
146
/// <param name="path">Path to the file.</param>
@@ -164,17 +168,39 @@ static LoggerConfiguration ConfigureFile(
164
168
long ? fileSizeLimitBytes = DefaultFileSizeLimitBytes ,
165
169
LoggingLevelSwitch levelSwitch = null ,
166
170
bool buffered = false ,
167
- bool propagateExceptions = false )
171
+ bool propagateExceptions = false ,
172
+ bool shared = false )
168
173
{
169
174
if ( addSink == null ) throw new ArgumentNullException ( nameof ( addSink ) ) ;
170
175
if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
171
176
if ( path == null ) throw new ArgumentNullException ( nameof ( path ) ) ;
172
177
if ( fileSizeLimitBytes . HasValue && fileSizeLimitBytes < 0 ) throw new ArgumentException ( "Negative value provided; file size limit must be non-negative" ) ;
173
178
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 ;
175
190
try
176
191
{
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
178
204
}
179
205
catch ( Exception ex )
180
206
{
0 commit comments