Skip to content

Feature/initial attempt #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f17cb52
initial serilog commit
Oct 10, 2018
fbeb47b
intial comments
Oct 10, 2018
28a52d6
test comment for reference
Oct 12, 2018
360de64
comments made around where to do compression
Oct 12, 2018
11f9662
comments on placement of compression
Oct 12, 2018
834b1b8
location to do compression o previous file
Oct 17, 2018
6a2884f
updates to comments on new method
Oct 17, 2018
1027296
file creation and deletion based off previous log
Oct 17, 2018
1e27cd8
basic zipping of previous log file at roll
Oct 17, 2018
62aca49
added parameters to rolling class, zip type set up in method
Oct 17, 2018
814e17f
paramters set up for compression and compression type
Oct 17, 2018
b2f32cf
GZIP method set up
Oct 17, 2018
67c1153
layout for gzip method set up
Oct 17, 2018
d58afda
gzip attempt
Oct 31, 2018
99f27a1
GZip basic functionality
Oct 31, 2018
9493330
check for uncompressd files and compress
Oct 31, 2018
c259969
remove compression bool
Oct 31, 2018
636b9f2
new Xunit file, error adding
Oct 31, 2018
fddd376
test written for checking compresison, need to delete extra created file
Nov 7, 2018
558fb65
log file still in use
Nov 7, 2018
64411b7
tests running, disposes log object now
Nov 7, 2018
91053dc
moved compression to new file
Nov 12, 2018
0be2657
initial commit of work on remote repo
Nov 12, 2018
14f3440
moved compression type into src
Nov 12, 2018
2d83c64
removed comments from methods
Nov 12, 2018
7489f97
moved compression tests into their own file
Nov 12, 2018
9a06121
added Assert that original .txt file is deleted in compression, roll
Nov 12, 2018
4b5ee31
completed initial PR comments, will resolve the rest on wednesday
Nov 12, 2018
a76b65b
updated for comments I did not have questions on / could implement now
Nov 16, 2018
4ccea1d
intial method to compress file in chunks for GZip
Nov 19, 2018
1325fa3
comments added to gzip method with current functionality
Nov 19, 2018
07b897a
updated gzip compress method to use chunking correctly
Nov 30, 2018
3b90756
add default value for compression type
Dec 3, 2018
5049097
replaced while loop with CopyTo() method
Dec 17, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/Serilog.Sinks.File/Serilog.Sinks.File.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@
</ItemGroup>

<ItemGroup>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<PackageReference Include="System.IO.Compression.ZipFile">
<Version>4.0.1</Version>
</PackageReference>
</ItemGroup>

</Project>
55 changes: 36 additions & 19 deletions src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,18 @@ CompressionType compressionType
_rollOnFileSizeLimit = rollOnFileSizeLimit;
}

/// <summary>
/// Chooses which compression algorithm to run on the log file passed in within the given directory.
/// </summary>
/// <param name="logFile"> The log file to be compressed </param>
/// <param name="logDirectory"> The directory that holds logFile </param>
/// <param name="compressionType"> The compression algorithm to be used. Is of type CompressionType </param>
public void Compress(string logFile, string logDirectory, CompressionType compressionType)
{
var readDirectory = Path.Combine(logDirectory, "new_dir");

{
switch (compressionType)
{
case CompressionType.Zip:
ZipCompress(logFile, logDirectory, readDirectory);
ZipCompress(logFile, logDirectory);
break;
case CompressionType.GZip:
GZipCompress(logFile, logDirectory);
Expand All @@ -87,39 +91,51 @@ public void Compress(string logFile, string logDirectory, CompressionType compre
}
}

public void ZipCompress(string prevLog, string logDirectory, string readDirectory)
/// <summary>
/// Uses Zip compression to compress prevLog into a new, zipped directory.
/// The Zip algorithm is from System.IO.Compression
/// </summary>
/// <param name="logFile"> The log file to be compressed </param>
/// <param name="logDirectory"> The directory that holds logFile </param>
public void ZipCompress(string logFile, string logDirectory)
{
var readDirectory = Path.Combine(logDirectory, "new_dir");
Directory.CreateDirectory(readDirectory);
System.IO.File.Move(
Path.Combine(logDirectory, prevLog), Path.Combine(readDirectory, prevLog)
Path.Combine(logDirectory, logFile), Path.Combine(readDirectory, logFile)
);

var zipName = prevLog.Remove(prevLog.Length - 4);
var zip_path = Path.Combine(logDirectory, $"{zipName}-Zip.zip");
var zipName = Path.GetFileNameWithoutExtension(logFile);
var zip_path = Path.Combine(logDirectory, $"{zipName}.zip");
System.IO.Compression.ZipFile.CreateFromDirectory(readDirectory, zip_path);

Directory.Delete(readDirectory, true);
}

public void GZipCompress(string prevLog, string logDirectory)
/// <summary>
/// Uses GZip compression algorithm to compress logFile into a .gzip file
/// The GZip algorithm is from System.IO.Compression
/// </summary>
/// <param name="logFile"> The log file to be compressed </param>
/// <param name="logDirectory"> The directory that holds logFile </param>
public void GZipCompress(string logFile, string logDirectory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why public?

{
var logPath = Path.Combine(logDirectory, prevLog);
byte[] byteArray;
using (FileStream prevLogStream = new FileStream(logPath, FileMode.Open))
{
byteArray = new byte[prevLogStream.Length];
prevLogStream.Read(byteArray, 0, (int)prevLogStream.Length);
}
var logPath = Path.Combine(logDirectory, logFile);
byte[] byteArray = new byte[] { };

byteArray = System.IO.File.ReadAllBytes(logPath);

var logName = prevLog.Remove(prevLog.Length - 4);
var GZipPath = Path.Combine(logDirectory, $"{logName}-GZip.gz");
var logName = Path.GetFileNameWithoutExtension(logFile);
var GZipPath = Path.Combine(logDirectory, $"{logName}.gz");

using (FileStream outFile = new FileStream(GZipPath, FileMode.Create))
using (System.IO.Compression.GZipStream gzipStream = new System.IO.Compression.GZipStream(outFile, System.IO.Compression.CompressionMode.Compress, false))
{
gzipStream.Write(byteArray, 0, byteArray.Length);
}

System.IO.File.WriteAllBytes(GZipPath, byteArray);

System.IO.File.Delete(logPath);
}

Expand Down Expand Up @@ -174,7 +190,8 @@ void AlignCurrentFileTo(DateTime now, bool nextSequence = false)
{
var directoryFileName = Path.GetFileName(directoryFile);

if (!(directoryFileName.Contains("-GZip")))
// Not sure if we can assume log files will always be .txt?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope 😄

The filename (and extension) are configurable by the user

if (System.IO.Path.GetExtension(directoryFileName) == ".txt")
{
Compress(directoryFileName, logDirectory, _compressionType);
}
Expand Down