logfmt.net 1.0.0

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

Logfmt.net

NuGet .NET License: MIT

logfmt.net is a simple, lightweight, and high-performance structured logging library for .NET applications, focusing on the logfmt format.

Features

  • High Performance: Optimized for low allocations and high throughput.
  • Modern .NET Support: Targets .NET 8.0 and .NET 10.0.
  • Encode & Decode: Write logfmt output and parse logfmt strings back into key-value pairs.
  • Standard Integrations:
    • Native support for Microsoft.Extensions.Logging.
    • Integration with OpenTelemetry.
  • Flexible Output: Writes to Console (stdout) by default, or any Stream.
  • Structured Data: First-class support for Key-Value pairs with typed values.

Installation

Install the package via NuGet:

dotnet add package logfmt.net

Usage

Basic Usage

using Logfmt;

var log = new Logger();
log.Info("Hello, World!");
// Output: ts=2026-03-21T12:00:00.0000000Z level=info msg="Hello, World!"

// With structured data using string pairs
log.Info("User logged in", "user_id", "123", "ip", "192.168.1.1");
// Output: ts=... level=info msg="User logged in" user_id=123 ip=192.168.1.1

// With typed values (int, bool, double, etc. — converted via ToString())
log.Info("Request handled", "status", 200, "duration_ms", 42);
// Output: ts=... level=info msg="Request handled" status=200 duration_ms=42

Default Fields with WithData

Use WithData() to create a logger that includes fields on every log entry:

var log = new Logger().WithData("service", "api", "env", "production");
log.Info("Server started", "port", "8080");
// Output: ts=... level=info msg="Server started" port=8080 service=api env=production

Severity Filtering

Control which log levels are emitted:

var log = new Logger(SeverityLevel.Warn);
log.Debug("This won't be logged");  // zero-cost: no allocations
log.Warn("This will be logged");

Custom Output Stream

Write logs to any stream:

using var stream = File.OpenWrite("app.log");
var log = new Logger(stream, SeverityLevel.Info);
log.Info("Written to file");

Parsing logfmt

Parse logfmt-formatted strings back into key-value pairs:

var pairs = LogfmtParser.Parse("level=info msg=\"hello world\" user_id=123");
// Returns IReadOnlyList<KeyValuePair<string, string>>
// pairs[0] = { "level", "info" }
// pairs[1] = { "msg", "hello world" }
// pairs[2] = { "user_id", "123" }

Microsoft.Extensions.Logging

Logfmt.net integrates seamlessly with the standard .NET logging abstractions.

using Microsoft.Extensions.Logging;
using Logfmt.ExtensionLogging;

// Add to your ILoggingBuilder (e.g., in ASP.NET Core or Generic Host)
builder.Logging.ClearProviders();
builder.Logging.AddLogfmt();

// Or with configuration
builder.Logging.AddLogfmt(config =>
{
    config.LogLevel["Default"] = LogLevel.Information;
    config.LogLevel["Microsoft"] = LogLevel.Warning;
});

// Inject and use ILogger
public class MyService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    public void DoWork()
    {
        _logger.LogInformation("Processing request {RequestId}", 12345);
        // Output: ts=... level=info msg="Processing request 12345" RequestId=12345
    }
}

OpenTelemetry Support

You can use logfmt as an exporter for OpenTelemetry logs.

using OpenTelemetry.Logs;
using Logfmt.OpenTelemetryLogging;

builder.Logging.AddOpenTelemetry(options =>
{
    options.AddLogfmtConsoleExporter();
});

Building the Source

You can build the project using the .NET CLI:

dotnet build
dotnet test

Contributing

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct, and the process for submitting pull requests to us.

Please feel free to submit a Pull Request or open an Issue.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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 was computed.  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.0 32 3/22/2026
0.4.1 34 3/19/2026
0.4.0 205 12/4/2025
0.3.0 686 12/2/2025
0.2.0 263 8/18/2025
0.1.2-alpha 154 6/25/2024
0.1.1-alpha 132 5/2/2024
0.0.4-alpha 2,818 11/12/2021
0.0.3-alpha 382 9/9/2021
0.0.2-alpha 777 7/12/2019

1.0.0:
     - logfmt parser (LogfmtParser.Parse) for decoding logfmt strings into key-value pairs.
     - ILoggingBuilder.AddLogfmt() extension for ASP.NET Core, Generic Host, and Minimal APIs.
     - Typed value support (int, bool, double, etc.) via params object[] overloads.
     - Source Link, deterministic builds, and symbol package (.snupkg).
     - Removed ASP.NET Core FrameworkReference — usable by console apps and worker services.
     - Resolved all TODOs; public API surface audited and stable.
     - SeverityLevel.Off now correctly disables all logging.
     - Backslash escaping in values.
     - Formatter exception handling in ExtensionLogger.
     - 92 tests covering core, extensions, OpenTelemetry, parser, edge cases, and concurrency.

     0.4.1:
     - ~1.8x throughput improvement and ~70% reduction in per-call memory allocations.
     - Thread-safe output writes with shared lock across WithData-derived loggers.
     - Cached severity level strings to eliminate per-call enum formatting allocations.
     - ThreadStatic StringBuilder reuse with capacity cap to prevent memory retention.
     - Replaced LINQ hot-path allocations with direct iteration.
     - Fixed race condition in ExtensionLoggerProvider.CreateLogger.
     - Replaced Dictionary with pre-sized List in ConsoleLogExporter for lower overhead.

     0.4.0:
     - Significant performance improvements (reduced allocations and increased throughput).
     - Modernized codebase (Nullable Reference Types, File-scoped namespaces).
     - Added benchmarks project.
     - Added support for default data pairs to be included on every log output.
     - Output of log data to console (default) or specified stream.
     - Severity levels (DEBUG, INFO, WARN, ERROR).
     - Data properties on logging methods as KeyValuePairs.
     - Added support for Microsoft.Extensions.Logging interfaces.
     - Added support for OpenTelemetry Logging.
     - Added handling of values containing line breaks.
     - Added OpenTelemetry support for logfmt format.
     - Added support for net10.0