1- using System . IO . Compression ;
1+ using System . IO . Compression ;
22using System . Text ;
33using Serilog . Core ;
4+ using Serilog . Events ;
45using Xunit ;
56using Serilog . Formatting . Json ;
67using Serilog . Sinks . File . Tests . Support ;
@@ -146,7 +147,7 @@ public void OnOpenedLifecycleHookCanWrapUnderlyingStream()
146147 var path = tmp . AllocateFilename ( "txt" ) ;
147148 var evt = Some . LogEvent ( "Hello, world!" ) ;
148149
149- using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , null , false , gzipWrapper ) )
150+ using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , null , false , gzipWrapper , LevelAlias . Off ) )
150151 {
151152 sink . Emit ( evt ) ;
152153 sink . Emit ( evt ) ;
@@ -178,12 +179,12 @@ public static void OnOpenedLifecycleHookCanWriteFileHeader()
178179 var headerWriter = new FileHeaderWriter ( "This is the file header" ) ;
179180
180181 var path = tmp . AllocateFilename ( "txt" ) ;
181- using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter ) )
182+ using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter , LevelAlias . Off ) )
182183 {
183184 // Open and write header
184185 }
185186
186- using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter ) )
187+ using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter , LevelAlias . Off ) )
187188 {
188189 // Length check should prevent duplicate header here
189190 sink . Emit ( Some . LogEvent ( ) ) ;
@@ -203,7 +204,7 @@ public static void OnOpenedLifecycleHookCanCaptureFilePath()
203204 var capturePath = new CaptureFilePathHook ( ) ;
204205
205206 var path = tmp . AllocateFilename ( "txt" ) ;
206- using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , capturePath ) )
207+ using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , capturePath , LevelAlias . Off ) )
207208 {
208209 // Open and capture the log file path
209210 }
@@ -223,7 +224,7 @@ public static void OnOpenedLifecycleHookCanEmptyTheFileContents()
223224 sink . Emit ( Some . LogEvent ( ) ) ;
224225 }
225226
226- using ( var sink = new FileSink ( path , new JsonFormatter ( ) , fileSizeLimitBytes : null , encoding : new UTF8Encoding ( false ) , buffered : false , hooks : emptyFileHook ) )
227+ using ( var sink = new FileSink ( path , new JsonFormatter ( ) , fileSizeLimitBytes : null , encoding : new UTF8Encoding ( false ) , buffered : false , hooks : emptyFileHook , LevelAlias . Off ) )
227228 {
228229 // Hook will clear the contents of the file before emitting the log events
229230 sink . Emit ( Some . LogEvent ( ) ) ;
@@ -235,6 +236,83 @@ public static void OnOpenedLifecycleHookCanEmptyTheFileContents()
235236 Assert . Equal ( '{' , lines [ 0 ] [ 0 ] ) ;
236237 }
237238
239+ [ Fact ]
240+ public void WhenFlushAtMinimumLevelIsNotReachedLineIsNotFlushed ( )
241+ {
242+ using var tmp = TempFolder . ForCaller ( ) ;
243+ var path = tmp . AllocateFilename ( "txt" ) ;
244+ var formatter = new JsonFormatter ( ) ;
245+
246+ using ( var sink = new FileSink ( path , formatter , null , null , true , null , LogEventLevel . Fatal ) )
247+ {
248+ sink . Emit ( Some . LogEvent ( level : LogEventLevel . Information ) ) ;
249+
250+ var lines = ReadAllLinesShared ( path ) ;
251+ Assert . Empty ( lines ) ;
252+ }
253+
254+ var savedLines = System . IO . File . ReadAllLines ( path ) ;
255+ Assert . Single ( savedLines ) ;
256+ }
257+
258+ [ Fact ]
259+ public void WhenFlushAtMinimumLevelIsReachedLineIsFlushed ( )
260+ {
261+ using var tmp = TempFolder . ForCaller ( ) ;
262+ var path = tmp . AllocateFilename ( "txt" ) ;
263+ var formatter = new JsonFormatter ( ) ;
264+
265+ using ( var sink = new FileSink ( path , formatter , null , null , true , null , LogEventLevel . Fatal ) )
266+ {
267+ sink . Emit ( Some . LogEvent ( level : LogEventLevel . Fatal ) ) ;
268+
269+ var lines = ReadAllLinesShared ( path ) ;
270+ Assert . Single ( lines ) ;
271+ }
272+
273+ var savedLines = System . IO . File . ReadAllLines ( path ) ;
274+ Assert . Single ( savedLines ) ;
275+ }
276+
277+ [ Fact ]
278+ public void WhenFlushAtMinimumLevelIsOffLineIsNotFlushed ( )
279+ {
280+ using var tmp = TempFolder . ForCaller ( ) ;
281+ var path = tmp . AllocateFilename ( "txt" ) ;
282+ var formatter = new JsonFormatter ( ) ;
283+
284+ using ( var sink = new FileSink ( path , formatter , null , null , true , null , LevelAlias . Off ) )
285+ {
286+ sink . Emit ( Some . LogEvent ( level : LogEventLevel . Fatal ) ) ;
287+
288+ var lines = ReadAllLinesShared ( path ) ;
289+ Assert . Empty ( lines ) ;
290+ }
291+
292+ var savedLines = System . IO . File . ReadAllLines ( path ) ;
293+ Assert . Single ( savedLines ) ;
294+ }
295+
296+ [ Fact ]
297+ public void WhenFlushAtMinimumLevelIsReachedMultipleLinesAreFlushed ( )
298+ {
299+ using var tmp = TempFolder . ForCaller ( ) ;
300+ var path = tmp . AllocateFilename ( "txt" ) ;
301+ var formatter = new JsonFormatter ( ) ;
302+
303+ using ( var sink = new FileSink ( path , formatter , null , null , true , null , LogEventLevel . Error ) )
304+ {
305+ sink . Emit ( Some . LogEvent ( level : LogEventLevel . Information ) ) ;
306+ sink . Emit ( Some . LogEvent ( level : LogEventLevel . Fatal ) ) ;
307+
308+ var lines = ReadAllLinesShared ( path ) ;
309+ Assert . Equal ( 2 , lines . Length ) ;
310+ }
311+
312+ var savedLines = System . IO . File . ReadAllLines ( path ) ;
313+ Assert . Equal ( 2 , savedLines . Length ) ;
314+ }
315+
238316 static void WriteTwoEventsAndCheckOutputFileLength ( long ? maxBytes , Encoding encoding )
239317 {
240318 using var tmp = TempFolder . ForCaller ( ) ;
@@ -260,4 +338,21 @@ static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding enco
260338 size = new FileInfo ( path ) . Length ;
261339 Assert . Equal ( encoding . GetPreamble ( ) . Length + eventOuputLength * 2 , size ) ;
262340 }
341+
342+ private static string [ ] ReadAllLinesShared ( string path )
343+ {
344+ // ReadAllLines cannot be used here, as it can't read files even if they are opened with FileShare.Read
345+ using var fs = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ;
346+ using var reader = new StreamReader ( fs ) ;
347+
348+ string ? line ;
349+ List < string > lines = [ ] ;
350+
351+ while ( ( line = reader . ReadLine ( ) ) != null )
352+ {
353+ lines . Add ( line ) ;
354+ }
355+
356+ return [ .. lines ] ;
357+ }
263358}
0 commit comments