logfmt.net
1.0.0
dotnet add package logfmt.net --version 1.0.0
NuGet\Install-Package logfmt.net -Version 1.0.0
<PackageReference Include="logfmt.net" Version="1.0.0" />
<PackageVersion Include="logfmt.net" Version="1.0.0" />
<PackageReference Include="logfmt.net" />
paket add logfmt.net --version 1.0.0
#r "nuget: logfmt.net, 1.0.0"
#:package logfmt.net@1.0.0
#addin nuget:?package=logfmt.net&version=1.0.0
#tool nuget:?package=logfmt.net&version=1.0.0
Logfmt.net
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.
- Native support for
- 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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.Hosting (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging (>= 10.0.0)
- Microsoft.Extensions.Logging.Configuration (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- OpenTelemetry (>= 1.14.0)
-
net8.0
- Microsoft.Extensions.Hosting (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging (>= 10.0.0)
- Microsoft.Extensions.Logging.Configuration (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- OpenTelemetry (>= 1.14.0)
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