-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from serilog/dev
Release 3.4.0
- Loading branch information
Showing
18 changed files
with
663 additions
and
453 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,3 +186,4 @@ UpgradeLog*.htm | |
# Microsoft Fakes | ||
FakesAssemblies/ | ||
*.orig | ||
/sample/Sample/logs/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Serilog.Sinks.Seq Copyright 2017 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if DURABLE | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Serilog.Sinks.Seq.Durable | ||
{ | ||
sealed class BookmarkFile : IDisposable | ||
{ | ||
readonly FileStream _bookmark; | ||
|
||
public BookmarkFile(string bookmarkFilename) | ||
{ | ||
_bookmark = System.IO.File.Open(bookmarkFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); | ||
} | ||
|
||
public FileSetPosition TryReadBookmark() | ||
{ | ||
if (_bookmark.Length != 0) | ||
{ | ||
// Important not to dispose this StreamReader as the stream must remain open. | ||
var reader = new StreamReader(_bookmark, Encoding.UTF8, false, 128); | ||
var current = reader.ReadLine(); | ||
|
||
if (current != null) | ||
{ | ||
_bookmark.Position = 0; | ||
var parts = current.Split(new[] { ":::" }, StringSplitOptions.RemoveEmptyEntries); | ||
if (parts.Length == 2) | ||
{ | ||
return new FileSetPosition(long.Parse(parts[0]), parts[1]); | ||
} | ||
} | ||
} | ||
|
||
return FileSetPosition.None; | ||
} | ||
|
||
public void WriteBookmark(FileSetPosition bookmark) | ||
{ | ||
if (bookmark.File == null) | ||
return; | ||
|
||
using (var writer = new StreamWriter(_bookmark)) | ||
{ | ||
writer.WriteLine("{0}:::{1}", bookmark.NextLineStart, bookmark.File); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_bookmark.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Serilog.Sinks.Seq Copyright 2017 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if DURABLE | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using Serilog.Debugging; | ||
|
||
namespace Serilog.Sinks.Seq.Durable | ||
{ | ||
class FileSet | ||
{ | ||
readonly string _bookmarkFilename; | ||
readonly string _candidateSearchPath; | ||
readonly string _logFolder; | ||
|
||
const string InvalidPayloadFilePrefix = "invalid-"; | ||
|
||
public FileSet(string bufferBaseFilename) | ||
{ | ||
if (bufferBaseFilename == null) throw new ArgumentNullException(nameof(bufferBaseFilename)); | ||
|
||
_bookmarkFilename = Path.GetFullPath(bufferBaseFilename + ".bookmark"); | ||
_logFolder = Path.GetDirectoryName(_bookmarkFilename); | ||
_candidateSearchPath = Path.GetFileName(bufferBaseFilename) + "*.json"; | ||
} | ||
|
||
public BookmarkFile OpenBookmarkFile() | ||
{ | ||
return new BookmarkFile(_bookmarkFilename); | ||
} | ||
|
||
public string MakeInvalidPayloadFilename(HttpStatusCode statusCode) | ||
{ | ||
var invalidPayloadFilename = $"{InvalidPayloadFilePrefix}{statusCode}-{Guid.NewGuid():n}.json"; | ||
return Path.Combine(_logFolder, invalidPayloadFilename); | ||
} | ||
|
||
public void CleanUpInvalidPayloadFiles(long maxNumberOfBytesToRetain) | ||
{ | ||
try | ||
{ | ||
var candiateFiles = Directory.EnumerateFiles(_logFolder, $"{InvalidPayloadFilePrefix}*.json"); | ||
DeleteOldFiles(maxNumberOfBytesToRetain, candiateFiles); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SelfLog.WriteLine("Exception thrown while trying to clean up invalid payload files: {0}", ex); | ||
} | ||
} | ||
|
||
public string[] GetFiles() | ||
{ | ||
return Directory.GetFiles(_logFolder, _candidateSearchPath) | ||
.OrderBy(n => n) | ||
.ToArray(); | ||
} | ||
|
||
static void DeleteOldFiles(long maxNumberOfBytesToRetain, IEnumerable<string> files) | ||
{ | ||
var orderedFileInfos = from candiateFile in files | ||
let candiateFileInfo = new FileInfo(candiateFile) | ||
orderby candiateFileInfo.LastAccessTimeUtc descending | ||
select candiateFileInfo; | ||
|
||
var invalidPayloadFilesToDelete = WhereCumulativeSizeGreaterThan(orderedFileInfos, maxNumberOfBytesToRetain); | ||
|
||
foreach (var fileToDelete in invalidPayloadFilesToDelete) | ||
{ | ||
try | ||
{ | ||
fileToDelete.Delete(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SelfLog.WriteLine("Exception '{0}' thrown while trying to delete file {1}", ex.Message, fileToDelete.FullName); | ||
} | ||
} | ||
} | ||
|
||
static IEnumerable<FileInfo> WhereCumulativeSizeGreaterThan(IEnumerable<FileInfo> files, long maxCumulativeSize) | ||
{ | ||
long cumulative = 0; | ||
foreach (var file in files) | ||
{ | ||
cumulative += file.Length; | ||
if (cumulative > maxCumulativeSize) | ||
{ | ||
yield return file; | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.