Docs menu · ILoggerProvider (.Net API)
DocsIntegrations

ILoggerProvider (.Net API)

If your application uses Microsoft.Extensions.Logging (ILogger<T>), as ASP.NET Core, Blazor, worker services and Azure Functions do, the LlamaLogger.Microsoft.Extensions.Logging package sends your warnings and errors to Llama Logger without changing your logging code.

Setup

  1. Install the LlamaLogger.Microsoft.Extensions.Logging NuGet package (.NET 8 or later).
  2. Register Llama Logger in Program.cs:
using LlamaLogger.Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddLlamaLogger(
    builder.Configuration["LlamaLoggerAPIKey"]!,           // your project API key
    typeof(Program).Assembly.GetName().Version!.ToString(), // your application's version
    client => client.AddCustomData("Server", Environment.MachineName)); // optional: configure the LlamaLoggerClient

var app = builder.Build();

Pass your own application's version explicitly as shown. The overload without arguments, builder.Services.AddLlamaLogger(), reads LlamaLoggerAPIKey from configuration but reports the package's version instead of yours.

The configure callback receives the underlying LlamaLoggerClient, so you can use any SDK feature there, such as custom data, customizing entries or ignoring exception types.

Log as usual

public class OrderService(ILogger<OrderService> logger)
{
    public void Submit(Order order)
    {
        try
        {
            // ...
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Order {OrderId} failed", order.Id);
        }
    }
}

What gets sent

  • Entries logged at Warning and Error level are sent to Llama Logger. Other levels go only to your other logging providers.
  • When an exception is included, Llama Logger receives the full exception. Otherwise it receives the log message text.
  • When your application shuts down, the provider gives queued entries up to 20 seconds to send.

Store the API key

Keep your project API key out of source control:

  • Local development: dotnet user-secrets set "LlamaLoggerAPIKey" "<YourAPIKey>"
  • Azure App Service: add an application setting named LlamaLoggerAPIKey.
  • Azure Functions: add LlamaLoggerAPIKey to the Values section of local.settings.json for local runs, and as an application setting in Azure.
  • Anywhere else: set an environment variable named LlamaLoggerAPIKey.