Serilog.Sinks.File.Archive 1.0.1

Plugin for the Serilog File sink that archives completed log files, optionally compressing them.

Install-Package Serilog.Sinks.File.Archive -Version 1.0.1
dotnet add package Serilog.Sinks.File.Archive --version 1.0.1
<PackageReference Include="Serilog.Sinks.File.Archive" Version="1.0.1" />
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.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Serilog.Sinks.File.Archive, 1.0.1"
For F# scripts that support #r syntax, copy this into the source code to reference the package.

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 -Version 1.0.1

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:

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.

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:

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 -Version 1.0.1

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:

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.

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:

NuGet packages (1)

Showing the top 1 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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.1 4,357 6/26/2020
1.0.0 352 4/29/2020