using Microsoft.Extensions.Logging;
using Serilog;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace ZB.MOM.WW.CBDD.Tests.Benchmark;

internal static class Logging
{
    private static readonly Lazy<ILoggerFactory> LoggerFactoryInstance = new(CreateFactory);

    /// <summary>
    ///     Gets the shared logger factory for benchmarks.
    /// </summary>
    public static ILoggerFactory LoggerFactory => LoggerFactoryInstance.Value;

    /// <summary>
    ///     Creates a logger for the specified category type.
    /// </summary>
    /// <typeparam name="T">The logger category type.</typeparam>
    /// <returns>A logger for <typeparamref name="T" />.</returns>
    public static ILogger CreateLogger<T>()
    {
        return LoggerFactory.CreateLogger<T>();
    }

    private static ILoggerFactory CreateFactory()
    {
        var serilogLogger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

        return Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
        {
            builder.ClearProviders();
            builder.AddSerilog(serilogLogger, true);
        });
    }
}