adding web logging

alphons <alphons@heijden.com> 2 Mar 2026, 19:26
caca8bd4574c2bb0623dd6119a1331a577c10d61
2 files changed
  • VanDerHeijden.Logging.slnx
  • src/VanDerHeijden.Logging.Web/README.md
diff --git a/VanDerHeijden.Logging.slnx b/VanDerHeijden.Logging.slnx
index a202887..bbfcc3d 100644
--- a/VanDerHeijden.Logging.slnx
+++ b/VanDerHeijden.Logging.slnx
@@ -2,7 +2,8 @@
<Project Path="src/VanDerHeijden.Logging/VanDerHeijden.Logging.csproj" />
<Project Path="src/VanDerHeijden.Logging.File/VanDerHeijden.Logging.File.csproj" />
<Project Path="src/VanDerHeijden.Logging.MongoDb/VanDerHeijden.Logging.MongoDb.csproj" />
- <Project Path="src/VanDerHeijden.Logging.Sql/VanDerHeijden.Logging.Sql.csproj" />
<Project Path="src/VanDerHeijden.Logging.Redis/VanDerHeijden.Logging.Redis.csproj" />
+ <Project Path="src/VanDerHeijden.Logging.Sql/VanDerHeijden.Logging.Sql.csproj" />
+ <Project Path="src/VanDerHeijden.Logging.Web/VanDerHeijden.Logging.Web.csproj" />
<Project Path="tests/VanDerHeijden.Logging.File.Benchmarks/VanDerHeijden.Logging.File.Benchmarks.csproj" />
</Solution>
diff --git a/src/VanDerHeijden.Logging.Web/README.md b/src/VanDerHeijden.Logging.Web/README.md
index 3feda83..d993e3f 100644
--- a/src/VanDerHeijden.Logging.Web/README.md
+++ b/src/VanDerHeijden.Logging.Web/README.md
@@ -1,23 +1,121 @@
-# VanDerHeijden.Logging.Web
+# VanDerHeijden.Logging
-Web log writer for [VanDerHeijden.Logging](https://www.nuget.org/packages/VanDerHeijden.Logging).
+High-performance, low-allocation batched logging for .NET 10, built on Microsoft.Extensions.Logging.
+Log entries are enqueued to an in-memory Channel<T> and flushed asynchronously in configurable batches. No I/O occurs on the hot path (application code).
-Writes batched Web log entries to **daily rotating text files** using async file I/O with a 64 KB write buffer. A new file is opened automatically at midnight without restarting the application.
+Supports pluggable writers:
+- Daily rotating text files
+- MongoDB collections
+- SQL Server (SqlBulkCopy)
+- Redis lists (RPUSH)
+- Custom implementations
+
+**VanDerHeijden.Logging.Web** adds HTTP-context extensions for structured request logging.
+
+## Packages
+
+| Package | Description | NuGet Link |
+|----------------------------------|--------------------------------------------------|------------|
+| VanDerHeijden.Logging | Core abstractions & batched logger | [NuGet](https://www.nuget.org/packages/VanDerHeijden.Logging) |
+| VanDerHeijden.Logging.File | Daily rotating text file writer | (separate package) |
+| VanDerHeijden.Logging.Web | ILogger extensions with HttpContext scope | (separate package) |
+| VanDerHeijden.Logging.MongoDb | MongoDB collection writer | (separate) |
+| VanDerHeijden.Logging.Sql | SQL Server bulk insert writer | (separate) |
+| VanDerHeijden.Logging.Redis | Redis list writer | (separate) |
## Installation
```bash
-dotnet add package VanDerHeijden.Logging.Web
+dotnet add package VanDerHeijden.Logging
+dotnet add package VanDerHeijden.Logging.File # recommended text file sink
+dotnet add package VanDerHeijden.Logging.Web # for web request context logging
```
-## Usage
+## Core Setup (Program.cs)
+
+```csharp
+using VanDerHeijden.Logging.File;
+
+builder.Logging.AddBatchedFileLogger(options =>
+{
+ options.LogDirectory = "Logs";
+ options.BatchSize = 200;
+ options.MaxIdleMilliseconds = 4000;
+ options.FullMode = BatchFullMode.DropOldest; // or Wait
+ // Optional: custom file naming, buffer size, etc.
+});
+```
+
+## Web Logging Extensions Usage
+
+In controllers, services, endpoints:
```csharp
-builder.Logging.AddFileLogger(logDirectory: "Logs");
+using Microsoft.AspNetCore.Mvc;
+using VanDerHeijden.Logging.Web;
+
+[ApiController]
+[Route("api/orders")]
+public class OrdersController(ILogger<OrdersController> logger) : ControllerBase
+{
+ [HttpPost]
+ public IActionResult Create(OrderDto dto)
+ {
+ try
+ {
+ // business logic...
+ logger.LogInformationWithContext(
+ HttpContext,
+ "Order created {OrderId} by {User}",
+ 12345,
+ dto.UserId);
+
+ return Ok();
+ }
+ catch (Exception ex)
+ {
+ logger.LogErrorWithContext(
+ HttpContext,
+ "Failed to create order {OrderId}",
+ dto.Id);
+
+ return StatusCode(500);
+ }
+ }
+}
```
+Available extensions (all add structured scope):
+
+- `LogInformationWithContext(HttpContext?, string, params object?[])`
+- `LogWarningWithContext`
+- `LogErrorWithContext`
+- `LogDebugWithContext`
+
+Scope properties added (if HttpContext provided):
+- TraceId
+- RequestMethod
+- RequestUrl (full scheme/host/path/query)
+- ClientIp (X-Forwarded-For aware)
+- Referer
+- UserAgent
+- User (from ClaimsPrincipal)
+- SessionId (if session enabled)
+
+Falls back to plain log if context is null.
+
+## Features
+
+- Zero I/O / low-allocation on Log() calls
+- Configurable batch size & idle timeout
+- Background flush consumer
+- Daily file rotation (no restart needed)
+- Thread-safe, proxy-friendly (X-Forwarded-For)
+- Structured properties for sinks like Seq, ELK, etc.
## Repository
-[https://github.com/alphons/VanDerHeijden.Logging](https://github.com/alphons/VanDerHeijden.Logging)
+https://github.com/alphons/VanDerHeijden.Logging
+
+MIT license. Issues / PRs welcome.