Com.H.Extensions.Logging.File 1.3.0

dotnet add package Com.H.Extensions.Logging.File --version 1.3.0
                    
NuGet\Install-Package Com.H.Extensions.Logging.File -Version 1.3.0
                    
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="Com.H.Extensions.Logging.File" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Com.H.Extensions.Logging.File" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Com.H.Extensions.Logging.File" />
                    
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 Com.H.Extensions.Logging.File --version 1.3.0
                    
#r "nuget: Com.H.Extensions.Logging.File, 1.3.0"
                    
#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 Com.H.Extensions.Logging.File@1.3.0
                    
#: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=Com.H.Extensions.Logging.File&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Com.H.Extensions.Logging.File&version=1.3.0
                    
Install as a Cake Tool

Com.H.Extensions.Logging.File

A simple, lightweight file logger for .NET applications that automatically organizes logs by date in a hierarchical directory structure (YYYY/MM/DD) with optional auto-cleanup of old logs. Perfect for IIS web gardens and multi-process environments.

For source code and documentation, kindly visit the project's github page https://github.com/H7O/Com.H.Extensions.Logging.File

Features

  • Simple Integration: Implements ILogger and ILoggerProvider interfaces
  • Automatic Date Organization: Logs are stored in YYYY/MM/DD directory structure
  • Auto-Cleanup: Optional automatic deletion of old log directories based on retention period
  • Multi-Process Safe: Uses global mutex for cross-process synchronization (perfect for IIS web gardens)
  • Thread-Safe: Safe for concurrent writes from multiple threads and processes
  • Customizable Location: Specify your own base directory or use the application's base directory by default
  • Zero Configuration: Works out of the box with sensible defaults
  • UTC Timestamps: All log entries are timestamped in UTC with ISO 8601 format
  • Production Ready: Handles abandoned mutexes, file locking conflicts, and process crashes gracefully

Sample 1

Basic usage with default settings (logs to application base directory)

using Com.H.Extensions.Logging.File;
using Microsoft.Extensions.Logging;

// Create logger provider
ILoggerProvider loggerProvider = new SimpleFileLoggerProvider();

// Create logger
ILogger logger = loggerProvider.CreateLogger("MyApp");

// Log messages
logger.LogInformation("Application started");
logger.LogWarning("This is a warning message");
logger.LogError("This is an error message");

// Logs will be written to:
// <AppBaseDirectory>/YYYY/MM/DD/log.txt

Sample 2

Specifying a custom log directory

using Com.H.Extensions.Logging.File;
using Microsoft.Extensions.Logging;

// Specify custom base directory for logs
string logBasePath = @"C:\MyLogs";

ILoggerProvider loggerProvider = new SimpleFileLoggerProvider(logBasePath);
ILogger logger = loggerProvider.CreateLogger("MyApp");

logger.LogInformation("This will be logged to C:\\MyLogs\\YYYY\\MM\\DD\\log.txt");

Sample 3

Using with Microsoft.Extensions.DependencyInjection

using Com.H.Extensions.Logging.File;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

var services = new ServiceCollection();

services.AddLogging(builder =>
{
    builder.ClearProviders();
    builder.AddProvider(new SimpleFileLoggerProvider(@"C:\MyLogs"));
});

var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();

logger.LogInformation("Hello from dependency injection!");

Sample 4

Using with ASP.NET Core

In your Program.cs (ASP.NET Core 6+):

using Com.H.Extensions.Logging.File;

var builder = WebApplication.CreateBuilder(args);

// Clear default logging providers
builder.Logging.ClearProviders();

// Add file logging
builder.Logging.AddProvider(new SimpleFileLoggerProvider(@"C:\WebAppLogs"));

var app = builder.Build();

// Use ILogger in your controllers/services as usual
app.MapGet("/", (ILogger<Program> logger) =>
{
    logger.LogInformation("Hello from ASP.NET Core!");
    return "Hello World!";
});

app.Run();

Sample 5

Auto-cleanup of old logs (retain last 30 days)

using Com.H.Extensions.Logging.File;
using Microsoft.Extensions.Logging;

// Keep only the last 30 days of logs
// Older log directories will be automatically deleted once per day
ILoggerProvider loggerProvider = new SimpleFileLoggerProvider(
    baseDirectoryPath: @"C:\MyLogs",
    maxDaysToKeepLogs: 30
);

ILogger logger = loggerProvider.CreateLogger("MyApp");

logger.LogInformation("This log will be kept for 30 days");
// Cleanup happens automatically on the first log call each day

Auto-Cleanup Features:

  • Runs once per day on first log write
  • Efficient: Deletes entire year/month folders when possible
  • Production-safe: Silently ignores locked files, retries next day
  • Disabled by default: Pass null (default) or a value <= 0 to disable

Log Directory Structure

Logs are automatically organized in the following structure:

<BaseDirectory>
└── 2025
    └── 10
        └── 08
            └── log.txt

Log Entry Format

Each log entry is formatted as:

<ISO8601-UTC-Timestamp> [LogLevel] <Message>

Example:

2025-10-08T14:23:45.1234567Z [Information] Application started
2025-10-08T14:23:46.7891234Z [Warning] This is a warning message
2025-10-08T14:23:47.1122334Z [Error] This is an error message
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 is compatible.  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 is compatible.  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 is compatible.  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
1.3.0 199 10/9/2025
1.1.0 199 10/8/2025

Added multi-process support with global mutex synchronization (perfect for IIS web gardens). Handles abandoned mutexes and process crashes gracefully. Singleton logger instance for better resource management.