Skip to content

A lightweight logging library for .net applications, focusing on logfmt output

License

Notifications You must be signed in to change notification settings

khaines/logfmt.net

Repository files navigation

Logfmt.net

NuGet .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.
  • 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.

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: 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.1

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();

// 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
    }
}

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.

About

A lightweight logging library for .net applications, focusing on logfmt output

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages