This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Review and correct BOM marker handling in TryReadLine No longer drops lines if BOM marker detected and avoids offsetting the line start. Also adds more Selflog writes to try to catch bookmark read failures. * Extract Persistence methods into own classes Three classes have been created: - FileBufferDataProvider, with the responsability of tracking and retrieving buffered content on disk - FileBasedBookmarkProvider, with the responsability of reading from and writing to the bookmark file - InvalidPayloadLogger, reposnible for writing events to disk that failed to be processed (dead-letter queue type concept) For testing, the FS was abstracted through FileSystemAdapter An attempt mas made to seperate concerns as much as possible, and avoid refs / outs to limit tracking complexity. HttpLogShipper was pretty big and hard to follow along, with lots of state change. Eficiency was not considered yet in this refactoring effort. Tests were added to simulate multiple scenarios. Related to #25 * cleanup and review enhancements includes: - HRESULT exception handling comments and constants - SelfLog in case of IOException (locked buffer file) - refactor GetBatchOfEvents - fix for appVeyor package generation Test project targets net452 to work correctly with xUnit. See xunit/xunit#1130 (comment) for more info: add disposable interface to BookmarkProvider delete all the old buffer files (N-2) in one timer cycle read / create initial bookmark based on _retainedFileLimitCount * correct MoveForward behavior to respect retentionLimit * update readme
- Loading branch information
1 parent
30538f8
commit eaa7dd7
Showing
29 changed files
with
1,912 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Serilog.Sinks.Loggly/Sinks/Loggly/Durable/FileSetPosition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Serilog.Sinks.Loggly.Durable | ||
{ | ||
public class FileSetPosition | ||
{ | ||
public FileSetPosition(long position, string fileFullPath) | ||
{ | ||
NextLineStart = position; | ||
File = fileFullPath; | ||
} | ||
|
||
public long NextLineStart { get; } | ||
public string File { get; } | ||
|
||
public static readonly FileSetPosition None = default(FileSetPosition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/Serilog.Sinks.Loggly/Sinks/Loggly/FileBasedBookmarkProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using Serilog.Debugging; | ||
using Serilog.Sinks.Loggly.Durable; | ||
|
||
namespace Serilog.Sinks.Loggly | ||
{ | ||
class FileBasedBookmarkProvider : IBookmarkProvider | ||
{ | ||
readonly IFileSystemAdapter _fileSystemAdapter; | ||
readonly Encoding _encoding; | ||
|
||
readonly string _bookmarkFilename; | ||
Stream _currentBookmarkFileStream; | ||
|
||
public FileBasedBookmarkProvider(string bufferBaseFilename, IFileSystemAdapter fileSystemAdapter, Encoding encoding) | ||
{ | ||
_bookmarkFilename = Path.GetFullPath(bufferBaseFilename + ".bookmark"); | ||
_fileSystemAdapter = fileSystemAdapter; | ||
_encoding = encoding; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_currentBookmarkFileStream?.Dispose(); | ||
} | ||
|
||
public FileSetPosition GetCurrentBookmarkPosition() | ||
{ | ||
EnsureCurrentBookmarkStreamIsOpen(); | ||
|
||
if (_currentBookmarkFileStream.Length != 0) | ||
{ | ||
using (var bookmarkStreamReader = new StreamReader(_currentBookmarkFileStream, _encoding, false, 128, true)) | ||
{ | ||
//set the position to 0, to begin reading the initial line | ||
bookmarkStreamReader.BaseStream.Position = 0; | ||
var bookmarkInfoLine = bookmarkStreamReader.ReadLine(); | ||
|
||
if (bookmarkInfoLine != null) | ||
{ | ||
//reset position after read | ||
var parts = bookmarkInfoLine.Split(new[] {":::"}, StringSplitOptions.RemoveEmptyEntries); | ||
if (parts.Length == 2 && long.TryParse(parts[0], out long position)) | ||
{ | ||
return new FileSetPosition(position, parts[1]); | ||
} | ||
|
||
SelfLog.WriteLine("Unable to read a line correctly from bookmark file"); | ||
} | ||
else | ||
{ | ||
SelfLog.WriteLine( | ||
"For some unknown reason, we were unable to read the non-empty bookmark info..."); | ||
} | ||
} | ||
} | ||
|
||
//bookmark file is empty or has been misread, so return a null bookmark | ||
return null; | ||
} | ||
|
||
public void UpdateBookmark(FileSetPosition newBookmark) | ||
{ | ||
EnsureCurrentBookmarkStreamIsOpen(); | ||
|
||
using (var bookmarkStreamWriter = new StreamWriter(_currentBookmarkFileStream, _encoding, 128, true)) | ||
{ | ||
bookmarkStreamWriter.BaseStream.Position = 0; | ||
bookmarkStreamWriter.WriteLine("{0}:::{1}", newBookmark.NextLineStart, newBookmark.File); | ||
bookmarkStreamWriter.Flush(); | ||
} | ||
} | ||
|
||
void EnsureCurrentBookmarkStreamIsOpen() | ||
{ | ||
//this will ensure a stream is available, even if it means creating a new file associated to it | ||
if (_currentBookmarkFileStream == null) | ||
_currentBookmarkFileStream = _fileSystemAdapter.Open( | ||
_bookmarkFilename, | ||
FileMode.OpenOrCreate, | ||
FileAccess.ReadWrite, | ||
FileShare.Read); | ||
} | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.