LokiLoggingProvider 1.0.0-preview.3

.NET 6.0
This is a prerelease version of LokiLoggingProvider.
dotnet add package LokiLoggingProvider --version 1.0.0-preview.3
NuGet\Install-Package LokiLoggingProvider -Version 1.0.0-preview.3
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="LokiLoggingProvider" Version="1.0.0-preview.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LokiLoggingProvider --version 1.0.0-preview.3
#r "nuget: LokiLoggingProvider, 1.0.0-preview.3"
#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.
// Install LokiLoggingProvider as a Cake Addin
#addin nuget:?package=LokiLoggingProvider&version=1.0.0-preview.3&prerelease

// Install LokiLoggingProvider as a Cake Tool
#tool nuget:?package=LokiLoggingProvider&version=1.0.0-preview.3&prerelease

Loki Logging Provider

Send logs directly to Grafana Loki from your .NET application. The Loki Logging Provider is a simple library that plugs into the logging framework provided in .NET.

For more information about logging, please checkout Logging in .NET and Logging in .NET Core and ASP.NET Core.

Usage

Step 1. Install the Package

The Loki logging provider is available from Nuget.org. Add it to your project with the command;

dotnet add package LokiLoggingProvider --version <version>

Step 2. Add the Logging Provider

For host based apps using the WebApplicationBuilder introduced in .NET 6, add the Loki logging provider by calling the AddLoki() extension method on the WebApplicationBuilder.Logging builder instance.

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Logging.AddLoki();

For older host based apps, add the Loki logging provider by calling the AddLoki() extension method on the ILoggingBuilder instance when configuring your host.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logBuilder =>
            {
                logBuilder.AddLoki();
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
    }
}

For non-host based apps, add the Loki logging provider by calling the AddLoki() extension method when creating your ILoggerFactory.

using Microsoft.Extensions.Logging;

public class Program
{
    public static void Main()
    {
        using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
        {
            builder.AddLoki();
        });

        ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

        logger.LogInformation("Hello from my Console App!");
    }
}

Step 3. Configure Logging

By default, the Loki logging provider does nothing. You must specify the type of push client (Grpc or Http) to use before any logs will be sent to Loki. Configure the Loki logging provider by modifying appsettings.json or its variants. An example is below.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "Loki": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      },
      "Client": "Grpc",
      "Grpc": {
        "Address": "http://localhost:9095"
      },
      "StaticLabels": {
        "JobName": "Example.WebApp"
      }
    }
  }
}

For a complete list of all the options you can configure and their defaults, please checkout the LokiLoggerOptions class and its related classes. The Loki section in appsettings.json basically binds to this class.

Alternatively, you can configure the Loki logging provider by passing an Action delegate that configures an instance of LokiLoggerOptions to the AddLoki() extension method.

Examples

There are two example projects that use the Loki logging provider which you can use as a reference.

There is also a docker-compose.yml file which you can use to help spin up an instance of Grafana and Loki in Docker for local development purposes.

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
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-preview.3 3,732 6/19/2022
1.0.0-preview.2 71 6/19/2022
1.0.0-preview.1 1,634 7/20/2021