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";
}
}
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;
}
}
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();
}
}
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));
}
}
{
"LlamaLoggerAPIKey": "<YourAPIKey>"
}
If you're using this in an Azure function, put this in the "Values" section
{
"Values": {
"LlamaLoggerAPIKey" : "<YourAPIKey>"
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(builder => { builder.AddLlamaLogger(); }); //Add this line here
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<LlamaLoggerConfiguration>();
...