logfmt.net is a simple, lightweight, and high-performance structured logging library for .NET applications, focusing on the logfmt format.
- High Performance: Optimized for low allocations and high throughput.
- Modern .NET Support: Targets .NET 8.0 and .NET 10.0.
- 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.
Install the package via NuGet:
dotnet add package logfmt.netusing logfmt;
var log = new Logger();
log.Info("Hello, World!");
// Output: level=info msg="Hello, World!" ts=2023-10-27T10:00:00.0000000Z
// With structured data
log.Info("User logged in",
new KeyValuePair<string, string>("user_id", "123"),
new KeyValuePair<string, string>("ip", "192.168.1.1"));
// Output: level=info msg="User logged in" ts=... user_id=123 ip=192.168.1.1Logfmt.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();
// 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: level=info msg="Processing request 12345" ts=... RequestId=12345
}
}You can use logfmt as an exporter for OpenTelemetry logs.
using OpenTelemetry.Logs;
using Logfmt.OpenTelemetryLogging;
builder.Logging.AddOpenTelemetry(options =>
{
options.AddLogfmtConsoleExporter();
});You can build the project using the .NET CLI:
dotnet build
dotnet testContributions 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.