LokiLoggingProvider 1.0.0

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

// Install LokiLoggingProvider as a Cake Tool
#tool nuget:?package=LokiLoggingProvider&version=1.0.0                

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.

Warning: This package has not been optimized for high performance logging.

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 Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
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 (1)

Showing the top 1 popular GitHub repositories that depend on LokiLoggingProvider:

Repository Stars
dodyg/practical-aspnetcore
Practical samples of ASP.NET Core 9, 8.0, 7.0, 6.0, 5.0, 3.1, 2.2, and 2.1,projects you can use. Readme contains explanations on all projects.
Version Downloads Last updated
1.0.0 46,989 4/9/2023
1.0.0-preview.3 12,448 6/19/2022
1.0.0-preview.2 128 6/19/2022
1.0.0-preview.1 5,587 7/20/2021