Skip to content
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

Injection of zip-compressor for fileTarget #1423

Merged
merged 13 commits into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/NLog/NLog.Xamarin.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -396,6 +397,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.Xamarin.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -393,6 +394,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.doc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -406,6 +407,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -412,6 +413,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netfx35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -406,6 +407,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.netfx40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -406,6 +407,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
5 changes: 3 additions & 2 deletions src/NLog/NLog.netfx45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<FileAlignment>512</FileAlignment>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\NLog.snk</AssemblyOriginatorKeyFile>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<TargetFrameworkProfile></TargetFrameworkProfile>
<StyleCopTargetsFile>$(MSBuildExtensionsPath)\Microsoft\StyleCop\v4.4\Microsoft.StyleCop.Targets</StyleCopTargetsFile>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down Expand Up @@ -368,6 +367,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -412,6 +412,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.sl4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -413,6 +414,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.sl5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -410,6 +411,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.wp7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -409,6 +410,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.wp71.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -409,6 +410,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/NLog.wp8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@
<Compile Include="Targets\EventLogTargetOverflowAction.cs" />
<Compile Include="Targets\FileArchivePeriod.cs" />
<Compile Include="Targets\FileTarget.cs" />
<Compile Include="Targets\IFileCompressor.cs" />
<Compile Include="Targets\LineEndingMode.cs" />
<Compile Include="Targets\LogReceiverWebServiceTarget.cs" />
<Compile Include="Targets\MailTarget.cs" />
Expand Down Expand Up @@ -434,6 +435,7 @@
<Compile Include="Targets\Wrappers\SecurityLogOnType.cs" />
<Compile Include="Targets\Wrappers\SplitGroupTarget.cs" />
<Compile Include="Targets\Wrappers\WrapperTargetBase.cs" />
<Compile Include="Targets\ZipArchiveFileCompressor.cs" />
<Compile Include="Time\AccurateLocalTimeSource.cs" />
<Compile Include="Time\AccurateUtcTimeSource.cs" />
<Compile Include="Time\CachedTimeSource.cs" />
Expand Down
46 changes: 21 additions & 25 deletions src/NLog/Targets/FileTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ public class FileTarget : TargetWithLayoutHeaderAndFooter, ICreateFileParameters

private FileArchivePeriod archiveEvery;
private long archiveAboveSize;
#if NET4_5

private bool enableArchiveFileCompression;
#endif

/// <summary>
/// The filename if <see cref="FileName"/> is a fixed string
Expand Down Expand Up @@ -203,6 +202,13 @@ public FileTarget()
this.CleanupFileName = true;
}

#if NET4_5
static FileTarget()
{
FileCompressor = new ZipArchiveFileCompressor();
}
#endif

/// <summary>
/// Initializes a new instance of the <see cref="FileTarget" /> class.
/// </summary>
Expand Down Expand Up @@ -608,15 +614,22 @@ public int MaxArchiveFiles
/// <docgen category='Archival Options' order='10' />
public ArchiveNumberingMode ArchiveNumbering { get; set; }

#if NET4_5
/// <summary>
/// Used to compress log files during archiving.
/// This may be used to provide your own implementation of a zip file compressor,
/// on platforms other than .Net4.5.
/// Defaults to ZipArchiveFileCompressor on .Net4.5 and to null otherwise.
/// </summary>
public static IFileCompressor FileCompressor { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to compress archive files into the zip archive format.
/// </summary>
/// <docgen category='Archival Options' order='10' />
[DefaultValue(false)]
public bool EnableArchiveFileCompression
{
get { return enableArchiveFileCompression; }
get { return enableArchiveFileCompression && FileCompressor != null; }
set
{
enableArchiveFileCompression = value;
Expand All @@ -626,12 +639,7 @@ public bool EnableArchiveFileCompression
}
}
}
#else
/// <summary>
/// Gets or sets a value indicating whether to compress archive files into the zip archive format.
/// </summary>
private const bool EnableArchiveFileCompression = false;
#endif


/// <summary>
/// Gets or set a value indicating whether a managed file stream is forced, instead of used the native implementation.
Expand Down Expand Up @@ -1239,26 +1247,14 @@ private void ArchiveFile(string fileName, string archiveFileName)
string archiveFolderPath = Path.GetDirectoryName(archiveFileName);
if (!Directory.Exists(archiveFolderPath))
Directory.CreateDirectory(archiveFolderPath);

#if NET4_5

if (EnableArchiveFileCompression)
{
InternalLogger.Info("Archiving {0} to zip-archive {1}", fileName, archiveFileName);
using (var archiveStream = new FileStream(archiveFileName, FileMode.Create))
using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
using (var originalFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var zipArchiveEntry = archive.CreateEntry(Path.GetFileName(fileName));
using (var destination = zipArchiveEntry.Open())
{
originalFileStream.CopyTo(destination);
}
}

InternalLogger.Info("Archiving {0} to compressed {1}", fileName, archiveFileName);
FileCompressor.CompressFile(fileName, archiveFileName);
DeleteAndWaitForFileDelete(fileName);
}
else
#endif
{
InternalLogger.Info("Archiving {0} to {1}", fileName, archiveFileName);
File.Move(fileName, archiveFileName);
Expand Down
49 changes: 49 additions & 0 deletions src/NLog/Targets/IFileCompressor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright (c) 2004-2016 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.Targets
{
/// <summary>
/// <see cref="FileTarget"/> may be configured to compress archived files in a custom way
/// by setting <see cref="FileTarget.FileCompressor"/> before logging your first event.
/// </summary>
public interface IFileCompressor
{
/// <summary>
/// Create archiveFileName by compressing fileName.
/// </summary>
/// <param name="fileName">Absolute path to the log file to compress.</param>
/// <param name="archiveFileName">Absolute path to the compressed archive file to create.</param>
void CompressFile(string fileName, string archiveFileName);
}
}
Loading