Nilog
Typed structured logging extensions for Microsoft.Extensions.Logging, with optional Roslyn analyzers for C# projects.
Overview
Nilog adds WriteInformation, WriteWarning, WriteError and related methods to your existing ILogger. Use it when you want typed message-template calls without declaring a separate logging method for each message. Your application still chooses its logging providers, levels and destinations.
- Typed overloads for up to 16 arguments; additional arguments use
params object[]. - Named structured properties, scopes, exception reports and flush callback hooks.
- Optional
Nilog.Analyzersdiagnostics NILOG001–NILOG008 and an interpolation code fix. - Library targets for .NET 8, 9 and 10, with a build-time generator for higher-arity overloads.
Typed overloads avoid a call-site argument array. Enabled rendering, argument computation and provider processing can still allocate. The measurement methodology and limitations explain the recorded results; they are historical measurements rather than universal performance guarantees.
Quick start
From an existing .NET application’s directory:
dotnet add package Nilog
Use the ILogger already supplied by your application:
using Microsoft.Extensions.Logging;
using Nilog;
static void RecordOrder(ILogger logger, int orderId, decimal total)
{
logger.WriteInformation("Order {OrderId} total {Total:N2}", orderId, total);
}
The example defines a method; call it with your application’s logger. In a new console app, configure an ILogger provider separately, as shown in the runnable demo. Nilog requires no service registration or configuration file.
Requirements and installation
Use a project targeting .NET 8, 9 or 10 and a compatible Microsoft.Extensions.Logging provider.
The optional analyzer is available as a source project. A public Nilog.Analyzers package was unavailable from NuGet when checked on 2026-10-30. Follow the Functions project’s analyzer integration to reference it with OutputItemType="Analyzer" and ReferenceOutputAssembly="false"; adjust the project path for your application.
Read NuGet package guidance or browse published releases for package, DLL and demo downloads. Source development uses the exact SDK in global.json; this is distinct from a package consumer’s runtime requirements.
Usage
Keep message templates constant and pass values separately:
logger.WriteWarning("Queue {QueueName} contains {Count} messages", "orders", 120);
using (logger.WriteScope("OrderId", 42))
{
logger.WriteInformation("Processing order");
}
Use WriteTrace, WriteDebug, WriteInformation, WriteWarning, WriteError or WriteCritical for a fixed level. Use Nilogger.Log for a level determined at runtime. Check the API and exception overload reference when passing exceptions or values statically typed as object; compiler and call shape affect overload selection.
The optional source analyzers check supported call shapes for interpolation, template/value mismatches, concatenation, duplicate or positional names, misplaced exceptions, malformed templates and naming style. They do not perform general data-flow analysis.
Configuration
Keep using your application’s normal logging configuration for categories, levels, providers and sinks. Advanced Nilog cache, exception-report and flush hooks are documented in the full reference. Guard expensive argument computation with logger.IsEnabled(...) when necessary: C# evaluates arguments before calling Nilog.
Development and testing
Install the SDK pinned by global.json, plus the .NET runtimes for each target you intend to test. From the repository root:
dotnet restore Nilog/Nilog.sln --locked-mode
dotnet build Nilog/Nilog.Core/Nilog.Core.csproj -c Release --no-restore
dotnet test Nilog/Nilog.Tests/Nilog.Tests.csproj -c Release --no-restore
dotnet test Nilog/Nilog.Analyzers.Tests/Nilog.Analyzers.Tests.csproj -c Release --no-restore
dotnet run --project Nilog/Nilog.Demo -c Release -f net10.0
Run benchmarks separately with dotnet run --project Nilog/Nilog.Benchmark -c Release -f net10.0 -- --filter "*HighArityExtendedBenchmarks*". Reproduce workloads and report environment and provider behavior before interpreting timings. Keep SDK updates and dependency lock regeneration together.
The root CI workflow runs core and analyzer tests. The existing release workflow gates core package publication on core tests; it does not publish the analyzer package or rerun analyzer tests. The nested historical workflow is outside GitHub’s root workflow discovery.
Troubleshooting and limitations
- Use constant named templates instead of interpolation or concatenation to retain structured properties.
- Seventeen or more arguments use the params path; all-object arguments and compiler versions can affect overload binding.
- Enabled logging, scope value boxing, formatting, providers and expensive arguments can allocate.
FlushAsynccalls registered callbacks; register your buffering sink’s flush operation explicitly.- Exception reports are not a redaction boundary. The original exception reaches the provider; applications must remove secrets and personal data.
- Library AOT compatibility analysis does not establish compatibility of a complete host. Publish and test your application with its actual providers.
See detailed limits, cache behavior and measurement qualifications and the security policy.
Contributing and support
Read CONTRIBUTING.md. For issues, include target framework, compiler version, provider and a minimal call shape. Follow the security policy for private vulnerability reports.
Documentation index · Full reference · Changelog · Repository standards
License
MIT, copyright Gehan Fernando. See LICENSE.
Detailed reference
The full reference preserves the detailed API guide, historical measurements or screenshots, and examples previously included here.