PH.NlogExtensions
0.0.7
dotnet add package PH.NlogExtensions --version 0.0.7
NuGet\Install-Package PH.NlogExtensions -Version 0.0.7
<PackageReference Include="PH.NlogExtensions" Version="0.0.7" />
<PackageVersion Include="PH.NlogExtensions" Version="0.0.7" />
<PackageReference Include="PH.NlogExtensions" />
paket add PH.NlogExtensions --version 0.0.7
#r "nuget: PH.NlogExtensions, 0.0.7"
#:package PH.NlogExtensions@0.0.7
#addin nuget:?package=PH.NlogExtensions&version=0.0.7
#tool nuget:?package=PH.NlogExtensions&version=0.0.7
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 (
WrapperTargetBasetargets likeAsyncWrapper,BufferingWrapper, etc.) by recursively traversing down to the underlyingFileTarget. - Zip Compression: Archive active log files or the entire log directory directly into ZIP streams or byte arrays using
System.IO.CompressionandPH.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 | Versions 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. |
-
.NETStandard 2.0
- NLog (>= 5.0.5)
- PH.CompressionUtility (>= 0.0.1)
- System.IO.Compression.ZipFile (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.