Serilog.Sinks.File.Archive 1.0.3

.NET 6.0 .NET Standard 1.6 .NET Framework 4.5
dotnet add package Serilog.Sinks.File.Archive --version 1.0.3
NuGet\Install-Package Serilog.Sinks.File.Archive -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.File.Archive" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sinks.File.Archive --version 1.0.3
#r "nuget: Serilog.Sinks.File.Archive, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Sinks.File.Archive as a Cake Addin
#addin nuget:?package=Serilog.Sinks.File.Archive&version=1.0.3

// Install Serilog.Sinks.File.Archive as a Cake Tool
#tool nuget:?package=Serilog.Sinks.File.Archive&version=1.0.3

Serilog.Sinks.File.Archive

NuGet Build status

A FileLifecycleHooks-based plugin for the Serilog File Sink that works with rolling log files, archiving completed log files before they are deleted by Serilog's retention mechanism.

The following archive methods are supported:

  • Compress logs in the same directory (using GZip compression)
  • Copying logs to another directory
  • Compress logs (using GZip compression) and write them to another directory

Getting started

To get started, install the latest Serilog.Sinks.File.Archive package from NuGet:

Install-Package Serilog.Sinks.File.Archive

To enable archiving, use one of the new LoggerSinkConfiguration extensions that has a FileLifecycleHooks argument, and create a new ArchiveHooks. For example, to write GZip compressed logs to another directory (the directory will be created if it doesn't already exist):

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("my-app.log", hooks: new ArchiveHooks(CompressionLevel.Fastest, "C:\\My\\Archive\\Path"))
    .CreateLogger();

Or to copy logs as-is to another directory:

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("my-app.log", hooks: new ArchiveHooks(CompressionLevel.NoCompression, "C:\\My\\Archive\\Path"))
    .CreateLogger();

Or to write GZip compressed logs to the same directory the logs are written to:

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("my-app.log", hooks: new ArchiveHooks(CompressionLevel.Fastest))
    .CreateLogger();

Note that archival only works with rolling log files, as files are only deleted by Serilog's rolling file retention mechanism.

As is standard with Serilog, it's important to call Log.CloseAndFlush(); before your application ends.

Token Replacement

The targetDirectory constructor parameter supports replacement of tokens at runtime.

Tokens take the form {Name:FormatString}, where Name is the name of a supported token, and FormatString defines how the token value should be formatted.

At present, 2 tokens are supported, UtcDate and Date. These use standard .NET date format strings to insert components of the current date/time into the path. For example, you may wish to organise archived files into folders based on the current year and month:

new ArchiveHooks(CompressionLevel.Fastest, "C:\\Archive\\{UtcDate:yyyy}\\{UtcDate:MM}")

JSON appsettings.json configuration

It's also possible to enable archival when configuring Serilog from a configuration file using Serilog.Settings.Configuration. To do this, you will first need to create a public static class that can provide the configuration system with a configured instance of ArchiveHooks:

using Serilog.Sinks.File.Archive;

namespace MyApp.Logging
{
    public class SerilogHooks
    {
        public static ArchiveHooks MyArchiveHooks => new ArchiveHooks(CompressionLevel.Fastest, "C:\\My\\Archive\\Path");
    }
}

The hooks argument in Your appsettings.json file should be configured as follows:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "my-app.log",
          "fileSizeLimitBytes": 10485760,
          "rollOnFileSizeLimit": true,
          "retainedFileCountLimit": 5,
          "hooks": "MyApp.Logging.SerilogHooks::MyArchiveHooks, MyApp"
        }
      }
    ]
  }
}

To break this down a bit, what you are doing is specifying the fully qualified type name of the static class that provides your ArchiveHooks, using Serilog.Settings.Configuration's special :: syntax to point to the MyArchiveHooks member.

About FileLifecycleHooks

FileLifecycleHooks is a Serilog File Sink mechanism that allows hooking into log file lifecycle events, enabling scenarios such as wrapping the Serilog output stream in another stream, or capturing files before they are deleted by Serilog's retention mechanism.

Other available hooks include:

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Serilog.Sinks.File.Archive:

Package Downloads
Xfrogcn.AspNetCore.Extensions

AspNetCore extensions library,includes function as logging, HTTP requests, authentication token management, parallel queue processing, and more

Nefarius.Utilities.AspNetCore The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

My opinionated collection of utilities for ASP.NET Core applications.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Serilog.Sinks.File.Archive:

Repository Stars
GZTimeWalker/GZCTF
The GZ::CTF project, an open source CTF platform.
Version Downloads Last updated
1.0.3 14,335 11/22/2022
1.0.2 36,981 7/26/2022
1.0.1 182,259 6/26/2020
1.0.0 737 4/29/2020