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]); } }
/// <summary> /// Emit the provided log event to the sink. /// </summary> /// <param name="logEvent">The log event to write.</param> /// <remarks>Events that come in out-of-order (e.g. around the rollovers) /// may end up written to a later file than their timestamp /// would indicate.</remarks> public void Emit(LogEvent logEvent) { if (logEvent == null) { throw new ArgumentNullException("logEvent"); } lock (_syncRoot) { if (_isDisposed) { throw new ObjectDisposedException("The rolling file has been disposed."); } AlignCurrentFileTo(Clock.DateTimeNow); // If the file was unable to be opened on the last attempt, it will remain // null until the next checkpoint passes, at which time another attempt will be made to // open it. if (_currentFile != null) { _currentFile.Emit(logEvent); } } }
static void WriteTwoEventsAndCheckOutputFileLength(long?maxBytes, Encoding encoding) { using (var tmp = TempFolder.ForCaller()) { var path = tmp.AllocateFilename("txt"); var evt = Some.LogEvent("Irrelevant as it will be replaced by the formatter"); var actualEventOutput = "x"; var formatter = new FixedOutputFormatter(actualEventOutput); var eventOuputLength = encoding.GetByteCount(actualEventOutput); using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding)) { sink.Emit(evt); } var size = new FileInfo(path).Length; Assert.Equal(encoding.GetPreamble().Length + eventOuputLength, size); //write a second event to the same file using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding)) { sink.Emit(evt); } size = new FileInfo(path).Length; Assert.Equal(encoding.GetPreamble().Length + eventOuputLength * 2, size); } }
public static void OnOpenedLifecycleHookCanWriteFileHeader() { using (var tmp = TempFolder.ForCaller()) { var headerWriter = new FileHeaderWriter("This is the file header"); var path = tmp.AllocateFilename("txt"); using (new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, headerWriter)) { // Open and write header } using (var sink = new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, headerWriter)) { // Length check should prevent duplicate header here sink.Emit(Some.LogEvent()); } var lines = System.IO.File.ReadAllLines(path); Assert.Equal(2, lines.Length); Assert.Equal(headerWriter.Header, lines[0]); Assert.Equal('{', lines[1][0]); } }
public void FileControllerGetsCorrectString() { bool fileControllerGotLog = false; var fileController = new Mock <IFileController>(); fileController.Setup(f => f.AppendLogEntry("this is correct log")) .Callback(() => fileControllerGotLog = true); var sink = new FileSink(fileController.Object); sink.Emit("this is not correct log", Enums.LogLevel.Critical); Assert.False(fileControllerGotLog); sink.Emit("this is correct log", Enums.LogLevel.Trace); Assert.True(fileControllerGotLog); }
public void FileIsWrittenIfNonexistent() { using (var tmp = TempFolder.ForCaller()) { var nonexistent = tmp.AllocateFilename("txt"); var evt = Some.LogEvent("Hello, world!"); using (var sink = new FileSink(nonexistent, new JsonFormatter(), null)) { sink.Emit(evt); } var lines = System.IO.File.ReadAllLines(nonexistent); Assert.Contains("Hello, world!", lines[0]); } }
// Simplifications: // Events that come in out-of-order (e.g. around the rollovers) // may end up written to a later file than their timestamp // would indicate. public void Emit(LogEvent logEvent) { if (logEvent == null) { throw new ArgumentNullException("logEvent"); } lock (_syncRoot) { if (_isDisposed) { throw new ObjectDisposedException("The rolling file has been disposed."); } AlignCurrentFileTo(logEvent.Timestamp); _currentFile.Emit(logEvent); } }
public void WhenLimitIsNotSpecifiedFileSizeIsNotRestricted() { const int maxBytes = 5000; const int eventsToLimit = 10; using (var tmp = TempFolder.ForCaller()) { var path = tmp.AllocateFilename("txt"); var evt = Some.LogEvent(new string('n', maxBytes / eventsToLimit)); using (var sink = new FileSink(path, new JsonFormatter(), null)) { for (var i = 0; i < eventsToLimit * 2; i++) { sink.Emit(evt); } } var size = new FileInfo(path).Length; Assert.True(size > maxBytes * 2); } }
public void FileIsAppendedToWhenAlreadyCreated() { 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)) { sink.Emit(evt); } using (var sink = new FileSink(path, new JsonFormatter(), null)) { sink.Emit(evt); } var lines = System.IO.File.ReadAllLines(path); Assert.Contains("Hello, world!", lines[0]); Assert.Contains("Hello, world!", lines[1]); } }