Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
correct bookmark advance method, especially for situations where the …
Browse files Browse the repository at this point in the history
…read did not start on the first file in the set of buffer files on disk
  • Loading branch information
Miguel Alho authored and MiguelAlho committed Dec 3, 2018
1 parent dc5b6e2 commit d7b715b
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/Serilog.Sinks.Loggly/Sinks/Loggly/FileBufferDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void MoveBookmarkForward()
{
//move to next file
_currentBookmark = new FileSetPosition(0, fileSet[1]);
_bookmarkProvider.UpdateBookmark(_currentBookmark);
//we can also delete the previously read file since we no longer need it
_fileSystemAdapter.DeleteFile(fileSet[0]);
}
Expand All @@ -148,8 +147,7 @@ public void MoveBookmarkForward()
_currentBookmark = _retainedFileCountLimit.Value >= fileSet.Length
? new FileSetPosition(0, fileSet[1])
: new FileSetPosition(0, fileSet[fileSet.Length - _retainedFileCountLimit.Value]);
_bookmarkProvider.UpdateBookmark(_currentBookmark);


//delete all the old files
foreach (var oldFile in fileSet.Take(fileSet.Length - _retainedFileCountLimit.Value))
{
Expand All @@ -158,10 +156,36 @@ public void MoveBookmarkForward()
}
else
{
//move to the next file and delete the current one
_currentBookmark = new FileSetPosition(0, fileSet[1]);
_bookmarkProvider.UpdateBookmark(_currentBookmark); //set the file bookmark to avoid
_fileSystemAdapter.DeleteFile(fileSet[0]);
//move to the NEXT file and delete the current one
//there is no garantee the the current one is the first, so we need to make
//sure we start reading the next one based on the current on
var currentBookMarkedFileInFileSet = Array.FindIndex(fileSet, f => f == _currentBookmark.File);

// not sure this can occur, but if for some reason the file is no longer in the list
// we should start from the beginning, maybe; being a bit defensive here due to
// https://github.com/serilog/serilog-sinks-loggly/issues/25
if (currentBookMarkedFileInFileSet == -1)
{
//if not in file set, use first in set (or none)
_currentBookmark = fileSet.Length > 0
? new FileSetPosition(0, fileSet[0])
: null;
}
else
{
//if index of current is not the last , use next; otherwise use none
_currentBookmark = currentBookMarkedFileInFileSet <= fileSet.Length - 2
? new FileSetPosition(0, fileSet[currentBookMarkedFileInFileSet + 1])
: null;
}

//also clear all the previous files in the set to avoid problems (and because
//they should no longer be considered); If no previous exists (index is -1);
//keep existing
for (int i = 0; i <= currentBookMarkedFileInFileSet; i++)
{
_fileSystemAdapter.DeleteFile(fileSet[i]);
}
}
}
}
Expand Down

0 comments on commit d7b715b

Please sign in to comment.