Nilog 1.0.6

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

⚑ Nilog

Allocation-aware, high-performance logging for Microsoft.Extensions.Logging

Same ILogger. Same {Named} templates. None of the garbage.

NuGet License: MIT .NET Disabled path 16-arg typed Enabled path Analyzer AOT

πŸ“– Full docs, recipes, and architecture: github.com/gcfernando/Nilog


⚑ What Nilog is

Nilog is a set of zero-allocation, strongly-typed extension methods on ILogger. It is not a new logging framework, provider, or transport β€” it sits on top of the Microsoft.Extensions.Logging (MEL) pipeline you already use, and every call still flows through your existing ILoggerFactory and provider(s) (Console, Serilog, NLog, OpenTelemetry, Seq, Application Insights, …). Swap LogInformation(...) for WriteInformation(...) and the call site stops allocating a params object[] on every call β€” nothing else about your logging setup changes.

The stock ILogger extensions allocate a params object[] on every single call β€” even when the level is switched off and the message is thrown straight in the bin. On a hot path that is millions of pointless allocations and a busy garbage collector.

Nilog swaps that array for a stack-only struct. A disabled call allocates nothing and returns in about 2 ns (measured, v1.0.6). LoggerMessage.Define/[LoggerMessage] achieve the same disabled-path cost; Nilog needs no per-message declaration.

using Nilog;

// 0–16 args: zero allocation when the level is disabled. Lower allocation than the conventional extensions when enabled.
logger.WriteInformation("User {UserId} ordered {Count} items", userId, count);

// Up to sixteen args β€” still zero-array typed, no object[] ever built (extended to 16 in v1.0.4)
logger.WriteInformation("User {UserId} bought {Sku} x{Qty} in {Region} via {Channel} ({Tier}) at {Ts} ref {Ref} batch {B}",
    userId, sku, qty, region, channel, tier, ts, refId, batch);

⚑ At a glance

πŸš€ Zero-alloc disabled path 0–16 typed args β†’ 0 bytes (proven by unit tests asserting exactly 0L allocated). Conventional Microsoft extensions cost 17–43 ns and 56–272 B per filtered call (v1.0.6 measurement).
πŸ†• 6–16 arg typed overloads (v1.0.4) Source-generated Write*/Nilogger.Log overloads now reach sixteen arguments β€” 0 bytes disabled; β‰ˆ2.8 ns vs 42.6 ns for the conventional extension at 9 args (v1.0.6 measurement).
πŸ†• Typed multi-pair scopes (v1.0.4) WriteScope<T1,T2>, WriteScope<T1,T2,T3>, WriteScope<T1,T2,T3,T4> β€” no dictionary allocation, no array copy for the most common scope shapes.
πŸ†• Compact exception report (v1.0.4) WriteErrorException(ex, more: false) β†’ < 300 B (down from β‰ˆ 992 B); single-line [Title] Type: Message summary.
πŸ”₯ Leaner than MEL extensions when enabled Less allocation than the conventional extensions at 1–6 args; equal at 9; not faster than LoggerMessage.Define/[LoggerMessage].

| πŸ†• Span-based rendering | Plain {Name} templates render through a stack-allocated Span<char> β€” no StringBuilder, no pool, no array. | | πŸ†• Nilog.Analyzers β€” 8 rules + code fix | NILOG001–NILOG008: interpolation (one-click fix), count mismatch, concatenated templates, duplicate, positional, exception-as-value, malformed, and non-PascalCase placeholders. | | πŸ†• WriteError/WriteCritical typed no-exception | logger.WriteError("Error {Id}", id) β†’ zero-array typed overload (no params fallback). | | πŸ”Œ True drop-in | Same ILogger, same {Named} templates, same structured output to every sink. | | 🧩 Zero setup | Just using Nilog; β€” no DI, no registration, no config. | | 🧯 Hardened formatting | Template mismatches fall back to raw text; a throwing ToString() on a typed argument is rendered as [ToString failed: ...]. Validation and provider exceptions still propagate. | | 🧡 Thread-safe & AOT-ready | No reflection. Safe under contention, friendly to trimming and Native AOT. | | πŸ”’ Bounded template cache | MaxTemplateCacheEntries (default 10,000) stops caching new entries instead of growing unboundedly. |


πŸ“Š Benchmarks

Honest summary. Nilog's clear advantage is the disabled path (0 B, ~2–3 ns versus 17–43 ns / 56–272 B for conventional Microsoft extension methods) and lower enabled-path allocation than conventional extensions at 1–6 typed args. Compared with Microsoft's optimized LoggerMessage.Define / [LoggerMessage], Nilog is equal on the disabled path and not faster when enabled (and allocates 3.6Γ— more than the source generator at 9 args). Nilog's value is convenience β€” no per-message declaration β€” at near-optimized cost.

Measured for v1.0.6 with BenchmarkDotNet 0.15.8 Β· .NET 10.0.12 Β· Intel Core Ultra 7 265H Β· Windows 11 Β· SDK 10.0.401, 3 launches Γ— (5 warmup + 12 iterations), from an external harness that references the packed Nilog 1.0.6 package. Single-digit-nanosecond differences (< ~1 ns) are timer/loop-overhead artifacts, not real differences.

Disabled path (level filtered off)

Typed args MEL extension LoggerMessage.Define [LoggerMessage] Nilog
1 17.2 ns / 56 B 0.6 ns / 0 B 0.1 ns / 0 B 2.0 ns / 0 B
3 22.7 ns / 104 B 1.5 ns / 0 B 0.9 ns / 0 B 1.9 ns / 0 B
6 38.3 ns / 200 B 2.1 ns / 0 B 1.7 ns / 0 B 2.0 ns / 0 B
9 42.6 ns / 272 B n/a 2.6 ns / 0 B 2.8 ns / 0 B

Enabled path (message rendered)

Typed args MEL extension LoggerMessage.Define [LoggerMessage] Nilog
1 53.0 ns / 88 B 22.6 ns / 32 B 22.7 ns / 32 B 29.4 ns / 56 B
3 106.1 ns / 152 B 72.0 ns / 48 B 77.3 ns / 48 B 81.1 ns / 104 B
6 240.6 ns / 280 B 220.8 ns / 280 B 231.1 ns / 280 B 156.8 ns / 208 B
9 380.7 ns / 376 B n/a 211.2 ns / 104 B 360.0 ns / 376 B
  • Arguments statically typed as object bind to the params overload β€” no improvement over Microsoft.
  • A single scope: BeginScope 21.9 ns / 240 B vs WriteScope("k", v) 11.5 ns / 80 B (scope creation only).
  • Parallel logging (50,000 calls, MediumRun): Microsoft 3.46 ms / 5.34 MB vs Nilog 3.54 ms / 3.82 MB β€” equal time, ~28% less allocation. The enabled path always allocates the rendered string; only the disabled path is allocation-free.
  • Real ASP.NET Core app (Kestrel, loopback): 1,696 B/request vs 1,833 B (βˆ’7.5%); throughput and latency not distinguishable from Microsoft in this run.

Do not extrapolate micro-benchmark ratios to application performance. Full methodology and limits: main README.


⚠️ Limitations

Nilog removes the call-site object[] allocation for common logging calls, but it does not make every logging scenario allocation-free. We list these honestly rather than overclaim.

Scenario Allocation
0–16 typed arguments, disabled path 0 bytes (raised from 0–8 in v1.0.4)
0–16 typed arguments, enabled path rendered message string only β€” stack-allocated span path, no array (disabled path is 0 B)
17+ arguments falls back to params object[]; the IsEnabled guard still fires before any work is done
Enabled logging may still allocate depending on the sink, formatter, and value types β€” Nilog cannot control what a downstream sink does
Dynamic / interpolated / concatenated templates each unique string grows the template cache (Nilog.Analyzers NILOG001/NILOG003 catch this at compile time)
FlushAsync real flush β€” awaits every callback registered via Nilogger.RegisterFlush(...); a zero-allocation no-op only when nothing is registered

Why some of these are by design, not bugs: Nilog is a thin, allocation-aware layer over ILogger. It deliberately does not own the sink, the transport, or the async pipeline β€” that is what makes it a true drop-in that works with any Microsoft.Extensions.Logging provider and any hosting/cloud platform. Allocation past the call site (string rendering, sink I/O) belongs to the formatter and sink you already chose.


πŸ—ΊοΈ Roadmap

Status of planned work. βœ… shipped Β· 🚧 in progress Β· πŸ”­ considering Β· β›” decided against.

Item Status Notes
Typed overloads to 16 arguments βœ… 1.0.4 Source generator now emits 6–16 arg zero-array overloads; 9-arg disabled: 0.45 ns / 0 B (469Γ— faster).
Typed multi-pair scope overloads βœ… 1.0.4 WriteScope<T1,T2>, WriteScope<T1,T2,T3>, WriteScope<T1,T2,T3,T4> β€” no dictionary allocation.
Compact exception report βœ… 1.0.4 moreDetailsEnabled: false now allocates < 300 B (down from β‰ˆ 992 B); gate test added.
Lift the typed-overload ceiling beyond 5 args βœ… 1.0.3 Source generator first emitted 6–8 arg zero-array overloads.
More analyzer rules beyond NILOG001 βœ… 1.0.3 Added NILOG002–NILOG008 β€” 1 β†’ 8 rules.
Ship Nilog.Analyzers as a standalone NuGet package βœ… 1.0.3 Development-dependency package; adds no runtime dependency.
Real FlushAsync for buffering sinks βœ… 1.0.3 RegisterFlush/UnregisterFlush; no-op only when nothing is registered.
Compiler-enforced Native AOT / trim safety βœ… 1.0.3 IsAotCompatible=true; removed a real Exception.TargetSite trim hazard.
Code-fix provider for NILOG001 βœ… 1.0.3 One-click rewrite of $"..." into a literal template + appended args.
Code fixes for NILOG002 / NILOG003 πŸ”­ Ambiguous to auto-rewrite safely; diagnostics ship without an auto-fix for now.
ILogger-free static sink adapters β›” decided against Would fork the API and undermine Nilog's "true drop-in ILogger" design.

If you need something here sooner, open an issue at github.com/gcfernando/Nilog/issues.


πŸ“¦ Install

dotnet add package Nilog
<PackageReference Include="Nilog" Version="1.0.6" />

Targets .NET 8.0, 9.0, and 10.0. Dependencies: Microsoft.Extensions.Logging.Abstractions and Microsoft.Extensions.ObjectPool. Native AOT / trimming friendly.


πŸš€ Quick start

using Microsoft.Extensions.Logging;
using Nilog; // <- that's the whole setup

ILogger logger = LoggerFactory
    .Create(b => b.AddConsole())
    .CreateLogger("App");

// Plain message β€” ~4.1 ns, 0 bytes
logger.WriteInformation("Service started");

// Structured, strongly-typed, zero array allocation (1–16 args)
logger.WriteInformation("User {UserId} signed in from {Ip}", 42, "10.0.0.1");

// Up to sixteen args β€” 6–16 source-generated in v1.0.4, zero array, zero boxing on disabled path
logger.WriteInformation("User {UserId} bought {Sku} x{Qty} in {Region} via {Channel} ({Tier})",
    userId, sku, qty, region, channel, tier);

// Exception with typed context β€” no array, no boxing
try { Risky(); }
catch (Exception ex)
{
    logger.WriteError("Checkout failed for cart {CartId}", ex, cartId);
}

No Nilog-specific configuration or DI registration is required for normal use. There is no service to register, no logger factory to construct, and no config file to maintain β€” just using Nilog; and the existing ILogger you already have.


βš™οΈ Configuration

Nilog has nothing to configure for normal use. Log levels, categories, and providers remain standard Microsoft.Extensions.Logging configuration and work exactly as they do today:

builder.Logging
    .SetMinimumLevel(LogLevel.Information)
    .AddFilter("Microsoft", LogLevel.Warning)
    .AddConsole();

A few optional, process-wide static hooks exist on Nilogger for advanced scenarios β€” ExceptionFormatter (custom exception rendering), MaxTemplateCacheEntries (template-cache ceiling, default 10,000), and RegisterFlush/UnregisterFlush/FlushAsync (draining a custom buffering sink). None of them are required; see the full configuration reference for details.


🧩 Structured logging

A single Nilog call carries the same structured contract as the framework's own templated logging β€” no array, no extra allocation:

logger.WriteInformation("User {UserId} bought {Sku} x{Qty}", 42, "A-100", 3);
What the sink receives Value
Rendered message User 42 bought A-100 x3
UserId / Sku / Qty 42 / "A-100" / 3
{OriginalFormat} User {UserId} bought {Sku} x{Qty}
Exception (if any) attached via the Exception-overload, not as a template value
Scope whatever WriteScope(...) wraps the call in

Any structured-aware sink β€” Serilog, Seq, OpenTelemetry, Application Insights β€” gets clean, queryable fields exactly as if you had used the framework's own templated logging.


πŸ”— Provider compatibility

Nilog produces standard Microsoft.Extensions.Logging state (IReadOnlyList<KeyValuePair<string, object>> + {OriginalFormat}) β€” it does not implement a provider or sink itself, so it works with any MEL provider: Console, Debug, Serilog, NLog, OpenTelemetry, Seq, Application Insights, and cloud-specific exporters (AWS/GCP). This is contract-compatibility through the real LoggerFactory/ILoggerProvider pipeline (verified by LoggingEngineInteropTests and real Serilog/OpenTelemetry integration tests) β€” Nilog does not ship direct integrations with any specific database, log store, or cloud service (for example MongoDB, Elasticsearch, SQL, or Oracle); those come from whichever MEL provider or sink you choose.


πŸ†š Nilog vs the alternatives

Every row is phrased so βœ… is always the good result (βœ… yes/good Β· ❌ no Β· βž– partial).

Question Microsoft ILogger Serilog Nilog
Plugs into your existing ILogger & DI? βœ… βž– βœ…
Supports {Named} templates + structured properties? βœ… βœ… βœ…
Avoids the object[] allocation per call (1–16 args)? ❌ ❌ βœ…
Allocates nothing when the level is disabled? ❌ ❌ βœ…
LoggerMessage speed with no boilerplate? ❌ ❌ βœ…
Built-in formatted exception report (compact + verbose)? βž– βž– βœ…
Typed multi-pair scope (no dict allocation)? ❌ ❌ βœ… (NEW v1.0.4)
Zero-allocation single-key scope object? ❌ ❌ βœ…
Catches the interpolation footgun at compile time? ❌ ❌ βœ… (Nilog.Analyzers)
Needs zero setup (just using Nilog;)? βœ… ❌ βœ…

🧭 Choosing the right method

I want to… Call Allocates?
Log a constant message logger.WriteInformation("Started") none
Log 1–16 structured values logger.WriteInformation("User {Id}", id) none (typed)
Log 17+ structured values logger.WriteInformation("{A} … {Q}", …) one object[]
Log an error with exception logger.WriteError("Failed {Id}", ex, id) none (typed)
Log an error without exception logger.WriteError("Bad request") none
Exception report β€” compact summary logger.WriteErrorException(ex, "Title") < 300 B
Exception report β€” full verbose logger.WriteErrorException(ex, "Title", more: true) report buffer only
Dynamic level at runtime Nilogger.Log(logger, level, "…", a, b) none for 0–16 typed
Attach 1-pair scope using (logger.WriteScope("Key", value)) { … } ~24 B (boxed value)
Attach 2–4 pair scope (typed, no dict) using (logger.WriteScope("K1", v1, "K2", v2)) { … } only boxed values
Catch $"..." mistakes at build time add the Nilog.Analyzers package n/a

Tip: Keep templates to ≀ 16 named holes to stay on the zero-array typed path.


✨ Features

Six levels, typed for 0–16 args, params for 17+

logger.WriteTrace("Polling queue, {Count} items", count);
logger.WriteDebug("Cache miss for key {Key}", key);
logger.WriteInformation("Order {OrderId} confirmed", orderId);
logger.WriteWarning("Retry {Attempt}/{Max} for {Job}", attempt, max, job);
logger.WriteError("Payment failed for {OrderId}", ex, orderId);
logger.WriteCritical("Database unreachable on {Host}", ex, host);

// Nine-arg typed (NEW in v1.0.4) β€” zero array, 0.45 ns / 0 B on disabled path
logger.WriteInformation("{A} {B} {C} {D} {E} {F} {G} {H} {I}", a, b, c, d, e, f, g, h, i);

// Up to sixteen args β€” all zero-array on the disabled path
logger.WriteInformation("User {UserId} bought {Sku} x{Qty} in {Region} via {Channel} ({Tier}) ref {Ref} at {Ts}",
    userId, sku, qty, region, channel, tier, refId, ts);

Runtime-level API β€” zero alloc for 0–16 typed args

LogLevel level = config.Verbose ? LogLevel.Debug : LogLevel.Information;
Nilogger.Log(logger, level, "Processing {JobId}", jobId);                       // ~4 ns, 0 B
Nilogger.Log(logger, level, "{A} {B} {C} {D} {E} {F} {G} {H} {I}", a, b, c, d, e, f, g, h, i); // still 0 B when disabled

Bounded template cache

// Prevent unbounded memory growth from interpolated templates
Nilogger.MaxTemplateCacheEntries = 10_000;  // default; new entries parsed but not cached beyond limit

πŸ” Static analysis β€” catch the structured-logging footguns at compile time

Every optimization above depends on the message argument being a stable string literal whose placeholders match its arguments. A few mistakes undo it all silently:

logger.WriteInformation($"User {id} signed in");  // compiles fine, silently undoes everything
logger.WriteInformation("{A} {B}", a);            // 2 holes, 1 arg β†’ renders raw, loses props
logger.WriteInformation("User " + id + " in");    // concatenation β†’ never a stable template

Nilog.Analyzers is a separate, opt-in package (not referenced by Nilog.Core) that catches all three at build time across every Nilog call shape:

<PackageReference Include="Nilog.Analyzers" Version="1.0.6" PrivateAssets="all" />
Rule Severity Catches Auto-fix
NILOG001 Warning An interpolated string ($"...") used as the message template. βœ…
NILOG002 Warning A template whose {Placeholder} count β‰  the number of arguments supplied. β€”
NILOG003 Warning A template built with string concatenation (+) or string.Format(...). β€”
NILOG004 Warning The same named {Placeholder} used twice (duplicate structured-property key). β€”
NILOG005 Info Positional {0} placeholders instead of named {Name} ones. β€”
NILOG006 Warning An Exception passed as a template value instead of the exception parameter. β€”
NILOG007 Warning A malformed template β€” an unclosed { or an empty {} placeholder. β€”
NILOG008 Info A placeholder name that is not PascalCase ({userId} β†’ {UserId}). β€”
logger.WriteInformation($"User {id} signed in");        // ❌ NILOG001 (+ one-click fix)
logger.WriteInformation("{A} {B}", a);                  // ❌ NILOG002 (2 placeholders, 1 arg)
logger.WriteInformation("User " + id + " in");          // ❌ NILOG003
logger.WriteInformation("{Id} retried {Id}", a, b);     // ❌ NILOG004 (duplicate {Id})
logger.WriteInformation("{0} {1}", a, b);               // πŸ”΅ NILOG005 (prefer named)
logger.WriteInformation("Failed {Error}", ex);          // ❌ NILOG006 (use the exception parameter)
logger.WriteInformation("Unclosed {Brace");             // ❌ NILOG007 (malformed)
logger.WriteInformation("User {userId}", id);           // πŸ”΅ NILOG008 (PascalCase)

logger.WriteInformation("User {UserId} signed in", id); // βœ… no diagnostic

Promote the correctness rules to build-breaking errors in CI (NILOG005/008 are Info-only style):

<WarningsAsErrors>$(WarningsAsErrors);NILOG001;NILOG002;NILOG003;NILOG004;NILOG006;NILOG007</WarningsAsErrors>

Or via .editorconfig for the whole repo: dotnet_diagnostic.NILOG001.severity = error.

It's syntax/semantics-based β€” it catches the mistake at the call site. Full details: Static analysis.

FlushAsync β€” real flush for buffering sinks

// A batching/buffering sink registers how to drain itself…
Nilogger.RegisterFlush(ct => myBatchingSink.FlushAsync(ct));

// …and shutdown awaits every registered sink. With nothing registered this is a
// zero-allocation no-op (returns Task.CompletedTask synchronously).
await Nilogger.FlushAsync(cancellationToken);

// Typed multi-pair scopes β€” no dictionary allocation, no array copy (NEW in v1.0.4)
using (logger.WriteScope("OrderId", orderId, "CustomerId", customerId, "Currency", "GBP"))
{
    logger.WriteInformation("Order opened");   // all three KVPs in scope
}

🏭 Production readiness

Concern Nilog answer
Thread safety volatile, Interlocked, and ConcurrentDictionary throughout
Trimming / Native AOT IsAotCompatible=true β€” trim/AOT analyzers run every build (warnings-as-errors), and the Native AOT compiler emits native code from Nilog.dll with zero warnings. No reflection.
Memory growth MaxTemplateCacheEntries stops caching at the limit instead of growing unboundedly
Idle CPU cost No background timer β€” the UTC timestamp cache refreshes lazily, only when an exception is formatted
Process shutdown A final UTC refresh runs automatically on ProcessExit; ShutdownUtcTimer() for deterministic teardown
Formatting robustness Bad template falls back to raw text; throwing ToString() on typed args is isolated; null renders as (null)
Sink compatibility IReadOnlyList<KVP> + {OriginalFormat} β€” works with Console, Serilog, OTel, Seq, App Insights
Supported frameworks .NET 8, 9, 10

πŸ“– API at a glance

// Extension methods on ILogger β€” Write* for all six levels (typed 0–16, params 17+)
void WriteInformation(this ILogger logger, string message, params object[] args);
void WriteInformation<T0>(this ILogger logger, string message, T0 arg0);
void WriteInformation<T0,T1>(this ILogger logger, string message, T0 arg0, T1 arg1);
void WriteInformation<T0,T1,T2>(this ILogger logger, string message, T0 arg0, T1 arg1, T2 arg2);
void WriteInformation<T0,T1,T2,T3>(this ILogger logger, string message, T0 arg0, T1 arg1, T2 arg2, T3 arg3);
void WriteInformation<T0,T1,T2,T3,T4>(this ILogger logger, string message, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
// …and 6–16-argument overloads source-generated by Nilog.SourceGenerators (extended to 16 in v1.0.4)
void WriteInformation<T0,…,T15>(this ILogger logger, string message, T0 arg0, …, T15 arg15);
// Identical shape for WriteTrace, WriteDebug, WriteWarning

// Error/Critical β€” without exception (typed, zero-array; 1–16 args)
void WriteError<T0,…,T15>(this ILogger logger, string message, T0 arg0, …, T15 arg15);
// With exception
void WriteError<T0,…,T15>(this ILogger logger, string message, Exception exception, T0 arg0, …, T15 arg15);
// Identical shape for WriteCritical

// Exception reports
void WriteErrorException(this ILogger logger, Exception ex,
    string title = "System Error", bool moreDetailsEnabled = false);  // basic: < 300 B (v1.0.4)

// Scopes β€” typed multi-pair overloads NEW in v1.0.4 (no dictionary, no array)
IDisposable WriteScope(this ILogger logger, string key, object value);
IDisposable WriteScope<T1,T2>       (this ILogger logger, string k1, T1 v1, string k2, T2 v2);
IDisposable WriteScope<T1,T2,T3>    (this ILogger logger, string k1, T1 v1, string k2, T2 v2, string k3, T3 v3);
IDisposable WriteScope<T1,T2,T3,T4> (this ILogger logger, string k1, T1 v1, string k2, T2 v2, string k3, T3 v3, string k4, T4 v4);
IDisposable WriteScope(this ILogger logger, IDictionary<string, object> context);

// Static runtime-level API (zero-array for 0–16 typed args)
void Nilogger.Log<T0,…,T15>(ILogger logger, LogLevel level, string message, T0 a, …, T15 p);

// Global settings
static int MaxTemplateCacheEntries { get; set; }            // default 10,000
static void ShutdownUtcTimer();

// Flush: real drain of registered buffering sinks (no-op when none registered)
static void RegisterFlush(Func<CancellationToken, Task> flush);
static bool UnregisterFlush(Func<CancellationToken, Task> flush);
static Task FlushAsync(CancellationToken token = default);

❓ FAQ

Is it really zero allocation? On the disabled path: yes β€” 0 bytes for 0–16 typed args (extended to 16 in v1.0.4; asserted by the test suite including DisabledPath_NineTypedArgs_AllocatesZeroBytes). On the enabled path Nilog still allocates the rendered string but avoids the object[] for typed args β€” less than the conventional extensions at 1–6 args, equal at 9 args (376 B each), and more than LoggerMessage.Define/[LoggerMessage] at 1–3 args (v1.0.6 measurement).

What about 9+ arguments? Typed overloads now reach sixteen (1–5 hand-written, 6–16 source-generated). A 9-arg disabled call allocates 0 B and runs in ~2.8 ns (v1.0.6 measurement; the conventional extension takes ~43 ns / 272 B). Only at 17+ args does Nilog fall back to params object[] β€” the same as the framework. Prefer ≀ 16 named holes on hot paths, or move extra context into a typed WriteScope (2–4 pairs) or dictionary scope.

Does it work with my logging engine / sink / cloud platform? Yes. Nilog only produces standard Microsoft.Extensions.Logging state (IReadOnlyList<KVP> + {OriginalFormat}), so it flows through any MEL provider β€” Console, Serilog, NLog, OpenTelemetry, Seq, Application Insights, AWS/GCP exporters β€” and runs anywhere .NET runs (containers, Azure Functions, AWS Lambda, Kubernetes). It adds no transport of its own, so there is nothing platform-specific to configure. This is verified, not asserted: LoggingEngineInteropTests runs Nilog through the real LoggerFactory + ILoggerProvider pipeline (the exact contract every engine integrates through) and checks the rendered message, {OriginalFormat}, named properties, exceptions, and level-filtering all arrive intact.

Is it AOT / trimming safe? Yes β€” generics, pooling, string.Format, stack-allocated spans; no reflection. Native AOT friendly.

What does Nilog.Analyzers check? Eight rules: NILOG001 (interpolated templates, with a one-click code fix), NILOG002 (placeholder/argument count mismatch), NILOG003 (concatenated or string.Format templates), NILOG004 (duplicate named placeholder), NILOG005 (positional placeholders, Info), NILOG006 (an exception passed as a template value), NILOG007 (malformed template), and NILOG008 (non-PascalCase placeholder name, Info) β€” across every Write*/Nilogger.Log call shape. It's a separate, opt-in package β€” not referenced by Nilog.Core β€” so installing Nilog never pulls it in automatically.


πŸ“„ License

MIT Β© Gehan Fernando. Full docs at github.com/gcfernando/Nilog.

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
1.0.6 117 10/10/2026
1.0.5 107 10/7/2026
1.0.4 160 6/22/2026
1.0.3 134 6/19/2026
1.0.2 142 6/16/2026
1.0.1 144 6/15/2026
1.0.0 139 6/13/2026

Audit-remediation release. Every item below is covered by a regression test or an external
harness; the core suite (252 tests) and analyzer suite (66 tests) pass on net8.0, net9.0 and net10.0.

### 🐛 Fixed

- **F-001** `WriteError`/`WriteCritical`/`Log` with a derived exception type or extra arguments
 on C# ≀ 12 bound to the generic overload and lost the exception. Runtime and generated
 overloads now attach a leading `Exception`. Verified in a 6-configuration consumer matrix.
- **F-004 / F-005** `NILOG001` code fix: arguments are inserted in the correct position (after the
 exception argument where present), brace escaping no longer doubles braces, and the fix is
 withheld where it cannot be proven semantics-preserving.
- **F-006** A throwing `ToString()`/`ISpanFormattable` on a typed argument no longer escapes the
 log call; it renders as `[ToString failed: Type threw Ex]`.
- **F-007** `FlushAsync` honours cancellation, preserves earlier failures in an `AggregateException`
 and observes abandoned callbacks.
- **F-008** The template cache is bounded by entry count and by template length. New public API:
 `MaxCachedTemplateLength`, `TemplateCacheCount`, `ClearTemplateCache()`.
- **F-009** Exception reports are bounded (message, depth, node and total length), single-line in
 compact mode, list all `AggregateException` inners, and a throwing user `ExceptionFormatter`
 falls back to the built-in report.
- **F-010** Dictionary/`IEnumerable` scopes validate keys (`ArgumentException` for null/blank) and
 treat `Count` as a hint only.
- **F-013** Null arguments render as `(null)`, matching Microsoft.Extensions.Logging.
- `SmallScopeWrapper` indexer threw `IndexOutOfRangeException` for an out-of-range index; it now throws `ArgumentOutOfRangeException` (found by coverage analysis).

### 📚 Documentation

- Benchmark tables replaced with v1.0.6 measurements against conventional MEL extensions, `LoggerMessage.Define` and `[LoggerMessage]`; the earlier "240x faster" style comparisons (conventional extension only) are retired. Nilog is equal to the optimized APIs on the disabled path and not faster on the enabled path.
- Removed the unsupported "full parity with SerilogAnalyzer" claim from the README, NuGet readme and analyzer package description.
- **F-003** The v1.0.5 "0 B under parallel load" claim could not be reproduced and is withdrawn.
 Re-measured: BenchmarkDotNet MediumRun 3.82 MB (Nilog) vs 5.34 MB (Microsoft); process-wide probe
 80 B vs 112 B per 1-arg call. The enabled path allocates the rendered string.
- **F-002** Documented that statically `object`-typed arguments bind to `params` and allocate.
- **F-011 / F-014** Replaced "never throws" and "immutable static state" wording; documented the
 raw-template fallback (Nilog) versus `FormatException` (MEL) and the `object[]`-path ownership.

### 🧪 Tests & tooling

- New regression suites (exception binding, cache bounds, flush, exception report, formatting
 robustness, scope validation, allocation characterization, code-fix behaviour).
- `Nilog.Benchmark --parallel-alloc` process-wide allocation probe.
- Coverage-gap tests (high-arity states, scope wrappers) and additional NILOG002 analyzer cases; `NILOG002` description corrected for the `params`/MEL path.
- Line coverage measured at 51% (branch 45%) before the gap tests; see the Phase 2 coverage report.

### 🔒 CI / supply chain (F-012)

- Actions pinned to commit SHAs, read-only default permissions, tag/version values passed via
 environment variables and validated, analyzer tests added to CI.
- Added `global.json`, NuGet lock files (`--locked-mode` in CI/release), `SECURITY.md` and a CycloneDX SBOM procedure; removed a duplicated nested workflow directory.
- `dotnet list package --vulnerable --include-transitive`: 0 advisories in all projects.
- Not done: package signing and provenance attestation (require credentials; nothing was published).

### 📦 Version

- Package and analyzer version bumped to 1.0.6.