PH.NlogExtensions 0.0.7

dotnet add package PH.NlogExtensions --version 0.0.7
                    
NuGet\Install-Package PH.NlogExtensions -Version 0.0.7
                    
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="PH.NlogExtensions" Version="0.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PH.NlogExtensions" Version="0.0.7" />
                    
Directory.Packages.props
<PackageReference Include="PH.NlogExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PH.NlogExtensions --version 0.0.7
                    
#r "nuget: PH.NlogExtensions, 0.0.7"
                    
#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.
#:package PH.NlogExtensions@0.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PH.NlogExtensions&version=0.0.7
                    
Install as a Cake Addin
#tool nuget:?package=PH.NlogExtensions&version=0.0.7
                    
Install as a Cake Tool

PH.NlogExtensions

PH.NlogExtensions is a high-performance, lightweight .NET library that extends the NLog framework with powerful log management, retrieval, and compression utilities.

Targeting netstandard2.0, it is fully compatible with both legacy .NET Framework applications (e.g., .NET 4.6.1+) and modern cross-platform .NET runtimes (e.g., .NET Core, .NET 5/6/7/8/9).


Features

  • Robust Target Unwrapping: Supports wrapped targets (WrapperTargetBase targets like AsyncWrapper, BufferingWrapper, etc.) by recursively traversing down to the underlying FileTarget.
  • Zip Compression: Archive active log files or the entire log directory directly into ZIP streams or byte arrays using System.IO.Compression and PH.CompressionUtility.
  • Asynchronous & Synchronous Support: Every extension method offers both asynchronous (*Async) and synchronous variants.
  • Easy Retrieval: Access active log data as strings, byte arrays, or streams by specifying the NLog target name.

Installation

Add the library to your project via the dotnet CLI:

dotnet add package PH.NlogExtensions

Or by adding a package reference in your .csproj:

<PackageReference Include="PH.NlogExtensions" Version="x.y.z" />

Configuration Example

Define your file targets in nlog.config (e.g., an asynchronous wrapper wrapping a file target):

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    
    <target xsi:type="AsyncWrapper" name="asyncFile">
      <target xsi:type="File" name="myLogFile" fileName="logs/app-${shortdate}.log" />
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="asyncFile" />
  </rules>
</nlog>

Usage Guide

All methods are exposed as extension methods on the NLog.Logger class.

1. Compressing & Archiving Log Files

Get Current Active Log Files as ZIP Archive

Compress all active log files into a ZIP archive:

using PH.NlogExtensions;

// Asynchronous (Byte Array)
byte[] zipBytes = await logger.GetCurrentLogFilesAsZipAsync(cancellationToken);

// Asynchronous (MemoryStream)
using (MemoryStream zipStream = await logger.GetCurrentLogFilesAsZipMemoryStreamAsync(cancellationToken))
{
    // Process stream...
}

// Synchronous (Byte Array)
byte[] zipBytesSync = logger.GetCurrentLogFilesAsZip();

// Synchronous (MemoryStream)
using (MemoryStream zipStreamSync = logger.GetCurrentLogFilesAsZipMemoryStream())
{
    // Process stream...
}
Get the Entire Log Directory as a ZIP Archive

Useful for archiving all logs in the directory including rotated or historical log files:

// Asynchronous (Byte Array)
byte[] zipBytes = await logger.GetWholeLogDirectoryAsZipAsync();

// Asynchronous (MemoryStream)
using (MemoryStream zipStream = await logger.GetWholeLogDirectoryZipAsStreamAsync())
{
    // Process stream...
}

// Synchronous (Byte Array)
byte[] zipBytesSync = logger.GetWholeLogDirectoryAsZip();

// Synchronous (MemoryStream)
using (MemoryStream zipStreamSync = logger.GetWholeLogDirectoryZipAsStream())
{
    // Process stream...
}

2. Reading and Extracting Active Log Contents

Read Single Log File by Target Name

Retrieve the contents of a specific target's log file as a string or byte array:

// Asynchronous - Read as String
string logText = await logger.ReadCurrentLogFileAsync("myLogFile", cancellationToken);

// Asynchronous - Get raw bytes
byte[] logBytes = await logger.GetCurrentLogFileAsync("myLogFile", cancellationToken);

// Synchronous - Read as String
string logTextSync = logger.ReadCurrentLogFile("myLogFile");

// Synchronous - Get raw bytes
byte[] logBytesSync = logger.GetCurrentLogFile("myLogFile");
Read All Active Log Files

Fetch all current log files configured in the active NLog configuration:

// Asynchronous (Key: File Name, Value: Byte Content)
Dictionary<string, byte[]> allLogs = await logger.GetAllCurrentLogFilesAsync(cancellationToken);

// Asynchronous (Key: FileInfo, Value: Byte Content)
Dictionary<FileInfo, byte[]> allLogsWithInfo = await logger.GetAllCurrentLogFilesWithInfoAsync(cancellationToken);

// Synchronous (Key: File Name, Value: Byte Content)
Dictionary<string, byte[]> allLogsSync = logger.GetAllCurrentLogFiles();

// Synchronous (Key: FileInfo, Value: Byte Content)
Dictionary<FileInfo, byte[]> allLogsWithInfoSync = logger.GetAllCurrentLogFilesWithInfo();

License

This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.7 111 6/24/2026
0.0.6 308 6/3/2025
0.0.5 1,301 3/1/2023
0.0.4 423 2/28/2023
0.0.3 600 11/22/2022
0.0.2 613 3/31/2021
0.0.1 548 3/31/2021