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
<PackageReference Include="Com.H.Extensions.Logging.File" Version="1.3.0" />
<PackageVersion Include="Com.H.Extensions.Logging.File" Version="1.3.0" />
<PackageReference Include="Com.H.Extensions.Logging.File" />
paket add Com.H.Extensions.Logging.File --version 1.3.0
#r "nuget: Com.H.Extensions.Logging.File, 1.3.0"
#:package Com.H.Extensions.Logging.File@1.3.0
#addin nuget:?package=Com.H.Extensions.Logging.File&version=1.3.0
#tool nuget:?package=Com.H.Extensions.Logging.File&version=1.3.0
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
ILoggerandILoggerProviderinterfaces - Automatic Date Organization: Logs are stored in
YYYY/MM/DDdirectory 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<= 0to 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 | 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 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Logging.Abstractions (>= 2.1.1)
-
net7.0
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.1)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
-
net9.0
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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.