LowCodeHub.Logging
0.0.3
See the version list below for details.
dotnet add package LowCodeHub.Logging --version 0.0.3
NuGet\Install-Package LowCodeHub.Logging -Version 0.0.3
<PackageReference Include="LowCodeHub.Logging" Version="0.0.3" />
<PackageVersion Include="LowCodeHub.Logging" Version="0.0.3" />
<PackageReference Include="LowCodeHub.Logging" />
paket add LowCodeHub.Logging --version 0.0.3
#r "nuget: LowCodeHub.Logging, 0.0.3"
#:package LowCodeHub.Logging@0.0.3
#addin nuget:?package=LowCodeHub.Logging&version=0.0.3
#tool nuget:?package=LowCodeHub.Logging&version=0.0.3
LowCodeHub.Logging
A production-oriented logging library for ASP.NET Core built on Serilog. One extension method configures structured logging, HTTP request enrichment, sensitive data masking, file/console sinks, Application Insights, forwarded headers, and self-diagnostics — with safe defaults and PII-conscious enrichment.
Why This Library?
| Feature | LowCodeHub.Logging | Raw Serilog Setup | Default ASP.NET Logging |
|---|---|---|---|
| Setup | One extension method | 50+ lines of config | Built-in but limited |
| HTTP enrichment | PII-conscious defaults | Manual middleware | Request logging only |
| Exception destructuring | Built-in — Refit, EF Core, SqlClient | Manual | Stack trace only |
| Sensitive data masking | Allowlisted query keys | Manual | None |
| File sink | Pre-configured rolling + retention | Manual setup | None |
| Application Insights | One toggle | Manual sink + DI | Separate SDK |
| Forwarded headers | Integrated proxy config | Separate middleware | Separate middleware |
| Self-diagnostics | Built-in Serilog SelfLog | Manual | None |
| Startup validation | Fail-fast on invalid config | Silent failures | Silent failures |
Installation
dotnet add package LowCodeHub.Logging
Quick Start
using LowCodeHub.Logging.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.AddEnhancedSerilogLogging(
loggingSection: "LoggingOptions",
telemetrySection: "TelemetryOptions");
var app = builder.Build();
app.UseHttpContextEnricher();
app.MapGet("/", () => "ok");
app.Run();
That's it. Your application now has structured console + file logging, HTTP request enrichment (with PII-safe defaults), exception destructuring for Refit/EF Core/SqlClient, and optional Application Insights — all from two lines of setup.
Table of Contents
- Configuration
- Exception Destructuring
- HTTP Context Enrichment
- How It Works
- Best Practices
- Requirements
- License
Configuration
Full Configuration Example
{
"LoggingOptions": {
"ServiceName": "orders-api",
"MinimumLogLevel": "Information",
"WriteToFile": true,
"ConsoleLogging": {
"UseCompactJson": true,
"Theme": "literate"
},
"FileLogging": {
"LogDirectory": "Logs",
"LogFileName": "app.log",
"RollingInterval": "Day",
"RetainedFileCountLimit": 14,
"SharedFile": true
},
"ExceptionHandling": {
"MaxDestructuringDepth": 3
},
"RequestEnrichment": {
"IncludeQueryString": false,
"IncludeAuthenticatedUser": false,
"IncludeUserRoles": false,
"IncludeUserAgent": true,
"IncludeReferer": false,
"AllowedQueryKeys": ["page", "pageSize"]
},
"ForwardedHeaders": {
"Enabled": true,
"ForwardLimit": 1,
"RequireHeaderSymmetry": true,
"KnownProxies": ["10.0.0.10"],
"KnownNetworks": ["10.244.0.0/16"]
},
"SelfLog": {
"Enabled": true,
"WriteToConsoleError": true,
"FilePath": "Logs/serilog-selflog.txt"
}
},
"TelemetryOptions": {
"Enabled": false,
"ConnectionString": ""
}
}
Logging Options
| Option | Default | Description |
|---|---|---|
ServiceName |
required | Service name added to every log entry |
MinimumLogLevel |
Information |
Minimum log level (Verbose, Debug, Information, Warning, Error, Fatal) |
WriteToFile |
false |
Enable file sink |
Console Logging
| Option | Default | Description |
|---|---|---|
UseCompactJson |
true |
Use compact JSON format (recommended for containers) |
OutputTemplate |
Serilog default | Custom output template (when not using compact JSON) |
Theme |
"literate" |
Console theme |
File Logging
| Option | Default | Description |
|---|---|---|
LogDirectory |
"Logs" |
Directory for log files |
LogFileName |
"log.txt" |
Log file name |
RollingInterval |
Day |
Rolling interval (Infinite, Year, Month, Day, Hour, Minute) |
RetainedFileCountLimit |
15 |
Number of log files to retain |
SharedFile |
true |
Allow shared file access (for multi-process scenarios) |
OutputTemplate |
Serilog default | Custom output template |
Request Enrichment
| Option | Default | Description |
|---|---|---|
IncludeQueryString |
false |
Log query string values |
IncludeAuthenticatedUser |
false |
Log authenticated user identity |
IncludeUserRoles |
false |
Log user role claims |
IncludeUserAgent |
true |
Log User-Agent header |
IncludeReferer |
false |
Log Referer header |
AllowedQueryKeys |
[] |
Allowlist for query string keys (only logged when IncludeQueryString is true) |
Forwarded Headers
| Option | Default | Description |
|---|---|---|
Enabled |
true |
Enable X-Forwarded-For / X-Forwarded-Proto handling |
ForwardLimit |
1 |
Max hops to process |
RequireHeaderSymmetry |
true |
Require header count symmetry |
KnownProxies |
[] |
Trusted proxy IP addresses |
KnownNetworks |
[] |
Trusted proxy networks (CIDR notation) |
Self-Diagnostics
| Option | Default | Description |
|---|---|---|
Enabled |
false |
Enable Serilog SelfLog for diagnosing sink/serialization failures |
WriteToConsoleError |
true |
Write SelfLog to stderr |
FilePath |
null |
Optional file path for SelfLog output |
Application Insights
| Option | Default | Description |
|---|---|---|
Enabled |
required | Enable Application Insights telemetry |
ConnectionString |
null |
Application Insights connection string |
When Enabled=true, the library registers AI telemetry and writes Serilog traces to the DI-managed TelemetryConfiguration.
Exception Destructuring
Built-in destructurers extract structured data from common exception types:
| Destructurer | Exception Type | What It Extracts |
|---|---|---|
ApiExceptionDestructurer |
Refit ApiException |
Status code, URI, content, reason phrase |
ApiExceptionDestructurer |
Refit ValidationApiException |
Validation errors |
DbUpdateExceptionDestructurer |
EF Core DbUpdateException |
Entity type, state, properties |
SqlExceptionDestructurer |
SqlException |
Error number, state, procedure, line |
These are registered automatically — no additional configuration needed.
HTTP Context Enrichment
app.UseHttpContextEnricher(); // register early in the middleware pipeline
The enricher adds contextual properties to every log entry during HTTP request processing. What gets logged depends on your RequestEnrichment configuration:
- Always: Request path, HTTP method, response status code, client IP
- Opt-in: Query string (allowlisted keys only), authenticated user, user roles, User-Agent, Referer
Health endpoint logs (/health path) are excluded by default.
How It Works
┌─────────────────────────────────────────────────────────┐
│ AddEnhancedSerilogLogging(loggingSection, telemetry) │
└─────────────────────┬───────────────────────────────────┘
│
┌─────────────────┼─────────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────────┐ ┌──────────────────┐
│ Serilog │ │ Enrichers │ │ Destructurers │
│ Sinks │ │ │ │ │
├──────────┤ ├──────────────┤ ├──────────────────┤
│Console │ │Service Name │ │ApiException │
│(compact │ │Correlation │ │DbUpdateException │
│ JSON) │ │HTTP Context │ │SqlException │
│ │ │Machine Name │ │ │
│File │ │Environment │ │ │
│(rolling) │ │Thread │ │ │
│ │ │ │ │ │
│App │ │ │ │ │
│Insights │ │ │ │ │
└──────────┘ └──────────────┘ └──────────────────┘
- Configuration validation — Options are validated at startup. Invalid configuration fails fast.
- Serilog bootstrap — Console + Debug sinks are configured first for startup logging.
- Host integration —
UseSerilog()replaces the default logger with the configured Serilog pipeline. - Request logging — Serilog's
UseSerilogRequestLogging()adds HTTP request logging middleware. - HTTP enrichment —
UseHttpContextEnricher()adds contextual properties per request.
Best Practices
- Set a real
ServiceNameper service. - Keep
IncludeQueryString=falseunless you truly need it. - If query logging is enabled, use
AllowedQueryKeysallowlist only. - Keep user identity fields disabled by default in public-facing APIs.
- Configure
KnownProxies/KnownNetworksin Kubernetes or behind ingress. - Enable
SelfLogin production to catch sink/serialization failures. - Use compact JSON logs in containerized environments.
- Keep file retention bounded to avoid disk exhaustion.
- Enable Application Insights only when you provide a valid connection string.
Requirements
- .NET 10 or later
Serilog.AspNetCore10.0+ (included as a dependency)Microsoft.ApplicationInsights.AspNetCore(included — optional, enable via config)
License
MIT © Ahmed Abuelnour
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Microsoft.Data.SqlClient (>= 7.0.1)
- Microsoft.EntityFrameworkCore (>= 10.0.7)
- Refit (>= 10.1.6)
- Serilog.AspNetCore (>= 10.0.0)
- Serilog.Enrichers.CorrelationId (>= 3.0.1)
- Serilog.Enrichers.Environment (>= 3.0.1)
- Serilog.Enrichers.Process (>= 3.0.0)
- Serilog.Enrichers.Sensitive (>= 2.1.0)
- Serilog.Exceptions (>= 8.4.0)
- Serilog.Formatting.Compact (>= 3.0.0)
- Serilog.Sinks.Async (>= 2.1.0)
- Serilog.Sinks.Console (>= 6.1.1)
- Serilog.Sinks.File (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.