public void OnOpenedLifecycleHookCanWrapUnderlyingStream() { var gzipWrapper = new GZipHooks(); using (var tmp = TempFolder.ForCaller()) { var path = tmp.AllocateFilename("txt"); var evt = Some.LogEvent("Hello, world!"); using (var sink = new FileSink(path, new JsonFormatter(), null, null, false, gzipWrapper)) { sink.Emit(evt); sink.Emit(evt); } // Ensure the data was written through the wrapping GZipStream, by decompressing and comparing against // what we wrote List <string> lines; using (var textStream = new MemoryStream()) { using (var fs = System.IO.File.OpenRead(path)) using (var decompressStream = new GZipStream(fs, CompressionMode.Decompress)) { decompressStream.CopyTo(textStream); } textStream.Position = 0; lines = textStream.ReadAllLines(); } Assert.Equal(2, lines.Count); Assert.Contains("Hello, world!", lines[0]); } }
public void WhenStreamWrapperSpecifiedIsUsedForRolledFiles() { var gzipWrapper = new GZipHooks(); var fileName = Some.String() + ".txt"; using (var temp = new TempFolder()) { string[] files; var logEvents = new[] { Some.InformationEvent(), Some.InformationEvent(), Some.InformationEvent() }; using (var log = new LoggerConfiguration() .WriteTo.File(Path.Combine(temp.Path, fileName), rollOnFileSizeLimit: true, fileSizeLimitBytes: 1, hooks: gzipWrapper) .CreateLogger()) { foreach (var logEvent in logEvents) { log.Write(logEvent); } files = Directory.GetFiles(temp.Path) .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) .ToArray(); Assert.Equal(3, files.Length); Assert.True(files[0].EndsWith(fileName), files[0]); Assert.True(files[1].EndsWith("_001.txt"), files[1]); Assert.True(files[2].EndsWith("_002.txt"), files[2]); } // Ensure the data was written through the wrapping GZipStream, by decompressing and comparing against // what we wrote for (var i = 0; i < files.Length; i++) { using (var textStream = new MemoryStream()) { using (var fs = System.IO.File.OpenRead(files[i])) using (var decompressStream = new GZipStream(fs, CompressionMode.Decompress)) { decompressStream.CopyTo(textStream); } textStream.Position = 0; var lines = textStream.ReadAllLines(); Assert.Equal(1, lines.Count); Assert.True(lines[0].EndsWith(logEvents[i].MessageTemplate.Text)); } } } }
public void Should_compress_rolling_log_files() { var gzipWrapper = new GZipHooks(); var logEvents = new[] { Some.LogEvent(), Some.LogEvent(), Some.LogEvent() }; using (var temp = TempFolder.ForCaller()) { var path = temp.AllocateFilename("gz"); // Use a rolling log file configuration with a 1-byte size limit, so we roll after each log event using (var log = new LoggerConfiguration() .WriteTo.File(path, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1, hooks: gzipWrapper) .CreateLogger()) { foreach (var logEvent in logEvents) { log.Write(logEvent); } } // Get all the files the rolling file sink wrote var files = Directory.GetFiles(temp.Path) .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) .ToArray(); // We should have a file for each entry in logEvents files.Length.ShouldBe(logEvents.Length); // Ensure the data was written through the wrapping GZipStream, by decompressing and comparing against // what we wrote for (var i = 0; i < files.Length; i++) { var lines = Utils.DecompressLines(files[i]); lines.Count.ShouldBe(1); lines[0].ShouldEndWith(logEvents[i].MessageTemplate.Text); } } }
public void Should_compress_log_file() { var gzipWrapper = new GZipHooks(); var logEvents = new[] { Some.LogEvent(), Some.LogEvent(), Some.LogEvent() }; using (var temp = TempFolder.ForCaller()) { var path = temp.AllocateFilename("gz"); using (var log = new LoggerConfiguration() .WriteTo.File(Path.Combine(path), hooks: gzipWrapper) .CreateLogger()) { foreach (var logEvent in logEvents) { log.Write(logEvent); } } // Ensure the data was written through the wrapping GZipStream, by decompressing and comparing against // what we wrote var lines = Utils.DecompressLines(path); lines.Count.ShouldBe(logEvents.Length); for (var i = 0; i < logEvents.Length; i++) { lines[i].ShouldEndWith(logEvents[i].MessageTemplate.Text); } } }