1- using System . IO . Compression ;
1+ using System . IO . Compression ;
22using System . Text ;
33using Serilog . Core ;
4- using Serilog . Events ;
54using Xunit ;
65using Serilog . Formatting . Json ;
76using Serilog . Sinks . File . Tests . Support ;
@@ -147,7 +146,7 @@ public void OnOpenedLifecycleHookCanWrapUnderlyingStream()
147146 var path = tmp . AllocateFilename ( "txt" ) ;
148147 var evt = Some . LogEvent ( "Hello, world!" ) ;
149148
150- using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , null , false , gzipWrapper , LevelAlias . Off ) )
149+ using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , null , false , gzipWrapper ) )
151150 {
152151 sink . Emit ( evt ) ;
153152 sink . Emit ( evt ) ;
@@ -179,12 +178,12 @@ public static void OnOpenedLifecycleHookCanWriteFileHeader()
179178 var headerWriter = new FileHeaderWriter ( "This is the file header" ) ;
180179
181180 var path = tmp . AllocateFilename ( "txt" ) ;
182- using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter , LevelAlias . Off ) )
181+ using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter ) )
183182 {
184183 // Open and write header
185184 }
186185
187- using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter , LevelAlias . Off ) )
186+ using ( var sink = new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , headerWriter ) )
188187 {
189188 // Length check should prevent duplicate header here
190189 sink . Emit ( Some . LogEvent ( ) ) ;
@@ -204,7 +203,7 @@ public static void OnOpenedLifecycleHookCanCaptureFilePath()
204203 var capturePath = new CaptureFilePathHook ( ) ;
205204
206205 var path = tmp . AllocateFilename ( "txt" ) ;
207- using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , capturePath , LevelAlias . Off ) )
206+ using ( new FileSink ( path , new JsonFormatter ( ) , null , new UTF8Encoding ( false ) , false , capturePath ) )
208207 {
209208 // Open and capture the log file path
210209 }
@@ -224,7 +223,7 @@ public static void OnOpenedLifecycleHookCanEmptyTheFileContents()
224223 sink . Emit ( Some . LogEvent ( ) ) ;
225224 }
226225
227- using ( var sink = new FileSink ( path , new JsonFormatter ( ) , fileSizeLimitBytes : null , encoding : new UTF8Encoding ( false ) , buffered : false , hooks : emptyFileHook , LevelAlias . Off ) )
226+ using ( var sink = new FileSink ( path , new JsonFormatter ( ) , fileSizeLimitBytes : null , encoding : new UTF8Encoding ( false ) , buffered : false , hooks : emptyFileHook ) )
228227 {
229228 // Hook will clear the contents of the file before emitting the log events
230229 sink . Emit ( Some . LogEvent ( ) ) ;
@@ -236,83 +235,6 @@ public static void OnOpenedLifecycleHookCanEmptyTheFileContents()
236235 Assert . Equal ( '{' , lines [ 0 ] [ 0 ] ) ;
237236 }
238237
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-
316238 static void WriteTwoEventsAndCheckOutputFileLength ( long ? maxBytes , Encoding encoding )
317239 {
318240 using var tmp = TempFolder . ForCaller ( ) ;
@@ -338,21 +260,4 @@ static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding enco
338260 size = new FileInfo ( path ) . Length ;
339261 Assert . Equal ( encoding . GetPreamble ( ) . Length + eventOuputLength * 2 , size ) ;
340262 }
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- }
358263}
0 commit comments