ILoggerProvider (.Net API)

  1. Install the LlamaLogger.Core nuget package in your Application.Blazor.Server project (https://www.nuget.org/packages/LlamaLogger.Core)
  2. Add the logging classes detailed below
  3. Add the LlamaLoggerAPIKey to your appsettings.json file
  4. In your Program.cs file, call the ConfigureLogging() method on the IHostBuilder and then call AddLlamaLogger() within the ILoggingBuilder
  5. In your Startup.cs file, register the LlamaLoggerConfiguration as a Singleton

Add 4 Logging Classes

Project Setup
Project details in Llama Logger with the API Key

LlamaLoggerConfiguration.cs

using System.Reflection;

public class LlamaLoggerConfiguration
{
    public string? APIKey { get; }
    public string? Version { get; }

    public LlamaLoggerConfiguration(IConfiguration config)
    {
        APIKey = config.GetValue<string>("LlamaLoggerAPIKey");
        Version = Assembly.GetExecutingAssembly().GetName()?.Version?.ToString() ?? "Unknown";
    }
}

LlamaLoggerExtensions.cs

using Microsoft.Extensions.DependencyInjection.Extensions;

public static class LlamaLoggerExtensions
{
    public static ILoggingBuilder AddLlamaLogger(this ILoggingBuilder builder)
    {
        builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, LlamaLoggerProvider>());
        return builder;
    }
}

LlamaLoggerProvider.cs

using LlamaLogger.Core;
using System.Collections.Concurrent;

[ProviderAlias("LlamaLogger")]
public sealed class LlamaLoggerProvider : ILoggerProvider
{
    private LlamaLoggerClient llamaloggerClient;
    private readonly ConcurrentDictionary<string, LlamaLoggerWrapper> _loggers = new(StringComparer.OrdinalIgnoreCase);

    public LlamaLoggerProvider(LlamaLoggerConfiguration config)
    {
        llamaloggerClient = new LlamaLoggerClient(config.APIKey, config.Version);
    }

    public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new LlamaLoggerWrapper(llamaloggerClient));

    public void Dispose()
    {
        _loggers.Clear();
        llamaloggerClient.Dispose();
    }
}

LlamaLoggerWrapper.cs

using LlamaLogger.Core;

public class LlamaLoggerWrapper : ILogger
{
    LlamaLoggerClient llamaloggerClient;
    public LlamaLoggerWrapper(LlamaLoggerClient client)
    {
        llamaloggerClient = client;
    }

    public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;

    public bool IsEnabled(LogLevel logLevel) => logLevel == LogLevel.Warning || logLevel == LogLevel.Error;

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
    {
        if (!IsEnabled(logLevel))
            return;

        if (exception != null)
            llamaloggerClient.LogError(exception);
        else if (state != null)
            llamaloggerClient.LogError(Convert.ToString(state));
    }
}

Add the LlamaLoggerAPIKey to your appsettings.json file

{
    "LlamaLoggerAPIKey": "<YourAPIKey>" 
}

If you're using this in an Azure function, put this in the "Values" section

{
    "Values": {
        "LlamaLoggerAPIKey" : "<YourAPIKey>"
    }
}

ConfigureLogging (Program.cs)

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
            webBuilder.UseStartup<Startup>();
        })
    .ConfigureLogging(builder => { builder.AddLlamaLogger(); }); //Add this line here

Register LlamaLoggerConfiguration (Startup.cs)

    public void ConfigureServices(IServiceCollection services) {
        services.AddSingleton<LlamaLoggerConfiguration>();
        ...
An error has occurred. This application may no longer respond until reloaded. Reload 🗙