CSharpEssentials.LoggerHelper.Sink.File 5.5.0

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

CSharpEssentials.LoggerHelper.Sink.File

Rolling JSON log files with configurable retention and dynamic per-property routing for CSharpEssentials.LoggerHelper.

Targets: net8.0 · net9.0 · net10.0 — Part of the CSharpEssentials.LoggerHelper ecosystem. Install only the sinks you need.


Install

dotnet add package CSharpEssentials.LoggerHelper
dotnet add package CSharpEssentials.LoggerHelper.Sink.File

Quick Setup — JSON

Add to appsettings.json:

{
  "LoggerHelper": {
    "ApplicationName": "MyApp",
    "Routes": [
      { "Sink": "File", "Levels": ["Information", "Warning", "Error", "Fatal"] }
    ],
    "Sinks": {
      "File": {
        "Path": "Logs",
        "RollingInterval": "Day",
        "RetainedFileCountLimit": 14
      }
    }
  }
}
// Program.cs
builder.Services.AddLoggerHelper(builder.Configuration);

var app = builder.Build();
app.UseLoggerHelper();   // ← required: activates sinks and registers middleware

Absolute paths work on both Windows and Linux:

  • Windows: "C:\\Logs\\MyApp" or "C:/Logs/MyApp"
  • Linux / Docker: "/var/log/myapp" or "logs" (relative to the working directory)

Quick Setup — Fluent API

using CSharpEssentials.LoggerHelper;
using CSharpEssentials.LoggerHelper.Sink.File;
using Serilog.Events;

builder.Services.AddLoggerHelper(b => b
    .WithApplicationName("MyApp")
    .AddRoute("File", LogEventLevel.Information, LogEventLevel.Warning, LogEventLevel.Error, LogEventLevel.Fatal)
    .ConfigureFile(f => {
        f.Path                  = "Logs";
        f.RollingInterval       = "Day";
        f.RetainedFileCountLimit = 14;
    })
);

var app = builder.Build();
app.UseLoggerHelper();   // ← required

What You'll See

Each log event is written as a single JSON line:

{"Timestamp":"2026-06-01T14:23:01.1230000+00:00","Level":"Information","MessageTemplate":"Order {OrderId} placed by {UserId}","Properties":{"OrderId":42,"UserId":"usr_99","ApplicationName":"MyApp","SourceContext":"OrdersController"}}

Fields at a glance (Serilog JsonFormatter):

Field Description
Timestamp Event timestamp (ISO 8601 with offset)
Level Log level
MessageTemplate Raw message template
Exception Exception string (present only when an exception is logged)
Properties Structured properties pushed via scope or call-site

Files are named log-YYYYMMDD.txt by default (log.txt with Infinite) and roll according to RollingInterval.


Dynamic File Routing by Property

Route logs into separate subdirectories based on any runtime property — ideal for multi-tenant apps, per-module separation, or environment-based routing.

JSON config

"Sinks": {
  "File": {
    "Path": "Logs",
    "RollingInterval": "Day",
    "FileNameProperty": "TenantId"
  }
}

How it works

// Logs with TenantId="acme" → Logs/acme/log-20260601.txt
using (_logger.BeginScope(new Dictionary<string, object?> { ["TenantId"] = "acme" }))
{
    _logger.LogInformation("Order processed");
    _logger.LogError("Payment failed");
}

// Logs without TenantId → Logs/log-20260601.txt  (base path, no subdirectory)
_logger.LogInformation("App started");

Fluent API

builder.Services.AddLoggerHelper(b => b
    .AddRoute("File", LogEventLevel.Information, LogEventLevel.Error)
    .ConfigureFile(f => {
        f.Path             = "Logs";
        f.FileNameProperty = "TenantId";
        f.MaxOpenFiles     = 128;   // LRU pool limit for open file handles (default: 64)
    })
);

Configuration Options

Property Type Default Description
Path string "Logs" Base directory for log files. Relative paths are resolved from the app working directory.
RollingInterval string "Day" When to start a new file: Minute, Hour, Day, Month, Year, Infinite (single file, never rolls). Serilog enum name, case-insensitive (day = Day); an invalid value throws InvalidOperationException listing the valid ones and the sink shows FAILED.
RetainedFileCountLimit int 7 How many rolled files to keep before the oldest is deleted.
Shared bool true Allow multiple processes (e.g. multiple app instances) to write to the same file.
FileNameProperty string? null Log event property used to create per-value subdirectories (e.g. "TenantId"). Values are sanitized for folder names (\ / : * ? " < > | become _, max 100 chars). See section above.
MaxOpenFiles int 64 Maximum number of simultaneously open file handles when using FileNameProperty. Oldest handles are closed on LRU eviction.
ThrottleInterval TimeSpan? null Minimum interval between events sent through this route (JSON "00:00:05"). Events inside the window are dropped before the send. null or 00:00:00 = no throttling. The window is per route, so with FileNameProperty it is route-wide (not per file name), and two routes using this sink throttle independently.

All logs are written in structured JSON format using Serilog's JsonFormatter. There is no plain-text mode for this sink — use the Console sink for human-readable output.


Troubleshooting

Symptom Likely Cause Fix
No file created app.UseLoggerHelper() missing, or write permission denied Add app.UseLoggerHelper() and verify the process has write access to Path
All logs in base folder (no subdirectory) FileNameProperty not set in scope before logging Wrap log calls with BeginScope containing the property
Too many open files OS error MaxOpenFiles too high for the OS limit Reduce MaxOpenFiles or raise the OS ulimit -n
Old files not deleted RetainedFileCountLimit reached but files are locked Check for other processes holding file handles
Sink shows FAILED in Dashboard/MCP Invalid RollingInterval (e.g. "Daily") or unwritable Path Use Minute, Hour, Day, Month, Year or Infinite and check folder permissions
Different levels end up in the same file One File sink writes everything routed to it into a single file set Limit what is written with the route Levels; per-level files are not supported

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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. 
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
5.5.0 143 10/25/2026
5.4.0 119 10/16/2026
5.3.3 144 10/16/2026
5.3.2 105 10/11/2026
5.3.1 110 10/10/2026
5.3.0 113 10/10/2026
5.2.11 102 10/9/2026
5.2.10 113 10/7/2026
5.2.9 110 10/6/2026
5.2.8 103 10/6/2026
5.2.7 110 10/6/2026
5.2.6 132 8/31/2026
5.2.4.1 131 8/27/2026
5.2.4 136 8/25/2026
5.2.3 126 8/25/2026
5.2.2.9 145 7/11/2026
5.2.2.8 142 7/11/2026
5.2.2.7 144 7/10/2026
5.2.2.6 145 7/6/2026
5.2.2.5 136 7/5/2026
Loading failed