checkout / checkout-sdk-net

Checkout SDK for Microsoft .NET
https://checkout.com
MIT License
21 stars 23 forks source link

Log provider improvements #375

Closed armando-rodriguez-cko closed 6 months ago

armando-rodriguez-cko commented 8 months ago
david-chandler-cko commented 8 months ago

What about a test to reproduce the original issue as well? Something like this:

[Fact]
public async Task ShouldCreateASingleLoggerInstanceForMultipleConcurrentRequests()
{
    LogProvider.SetLogFactory(_loggerFactory);
    Type[] loggerTypes = new[] { typeof(LogProviderTests), typeof(AnotherTestClass), typeof(NoInitializedType) };
    Task<ILogger>[] createLoggerTasks = Enumerable.Range(1, 50)
        .Select(async index =>
        {
            int randomDelayMs = new Random().Next(1, 5);
            await Task.Delay(randomDelayMs);
            return await Task.FromResult(LogProvider.GetLogger(loggerTypes[index % loggerTypes.Length]));
        }).ToArray();
    ILogger[] loggers = await Task.WhenAll(createLoggerTasks);
    Assert.Equal(loggerTypes.Length, loggers.Distinct().Count());
}
IanKemp commented 8 months ago

Any movement on this?

armando-rodriguez-cko commented 8 months ago

Any movement on this?

I have this task in the current sprint, but if I don't have time, next sprint I will try to implement it.

leandro-tylkovitch-cko commented 7 months ago

Since the logger factory already caches the logger instances, I think this class could be simplified right down to the following:

public static class LogProvider
{
    private static ILoggerFactory _loggerFactory = new LoggerFactory();

    public static void SetLogFactory(ILoggerFactory factory)
    {
        _loggerFactory?.Dispose();
        _loggerFactory = factory;
    }

    public static ILogger GetLogger(Type loggerType)
    {
        return loggerType is null ? NullLogger.Instance : _loggerFactory.CreateLogger(loggerType);
    }
}

David,

I tried your approach but Integration Testing is failing. I've added your proposed Unit Test for ShouldCreateASingleLoggerInstanceForMultipleConcurrentRequests and it's working OK with the actual propposal

david-chandler-cko commented 7 months ago

Since the logger factory already caches the logger instances, I think this class could be simplified right down to the following:

public static class LogProvider
{
    private static ILoggerFactory _loggerFactory = new LoggerFactory();

    public static void SetLogFactory(ILoggerFactory factory)
    {
        _loggerFactory?.Dispose();
        _loggerFactory = factory;
    }

    public static ILogger GetLogger(Type loggerType)
    {
        return loggerType is null ? NullLogger.Instance : _loggerFactory.CreateLogger(loggerType);
    }
}

David,

I tried your approach but Integration Testing is failing. I've added your proposed Unit Test for ShouldCreateASingleLoggerInstanceForMultipleConcurrentRequests and it's working OK with the actual propposal

Do you have an example of the failure when you simplify the code? What's the integration test trying to do?

david-chandler-cko commented 6 months ago

I can't approve this as it's the code I suggested.

david-chandler-cko commented 6 months ago

A couple of points:

  1. The suggested test got removed: https://github.com/checkout/checkout-sdk-net/pull/375#issuecomment-1980865741
  2. The code seems fine, but I don't think I should approve this PR as it's just the code that I suggested should be pasted in.
armando-rodriguez-cko commented 6 months ago

A couple of points:

  1. The suggested test got removed: Log provider improvements #375 (comment)
  2. The code seems fine, but I don't think I should approve this PR as it's just the code that I suggested should be pasted in.

Yes I forgot this file, sorry