| | | 1 | | using ConsoleLogStreaming.Core; |
| | | 2 | | using Elsa.Diagnostics.ConsoleLogs.Services; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Diagnostics.ConsoleLogs.Extensions; |
| | | 7 | | |
| | | 8 | | internal static class ConsoleLogProviderServiceCollectionExtensions |
| | | 9 | | { |
| | | 10 | | public static void DecorateConsoleLogProvider(this IServiceCollection services) |
| | | 11 | | { |
| | 724 | 12 | | var descriptor = services.LastOrDefault(x => x.ServiceType == typeof(IConsoleLogProvider)); |
| | 10 | 13 | | if (descriptor == null) |
| | 0 | 14 | | return; |
| | | 15 | | |
| | 10 | 16 | | services.TryAddSingleton<ElsaConsoleLogRecentBuffer>(); |
| | 10 | 17 | | services.Remove(descriptor); |
| | 10 | 18 | | services.Add(ServiceDescriptor.Describe( |
| | 10 | 19 | | typeof(IConsoleLogProvider), |
| | 9 | 20 | | sp => new ElsaConsoleLogProvider( |
| | 9 | 21 | | CreateProvider(sp, descriptor), |
| | 9 | 22 | | sp.GetRequiredService<IConsoleLogContextAccessor>(), |
| | 9 | 23 | | sp.GetRequiredService<ElsaConsoleLogRecentBuffer>()), |
| | 10 | 24 | | descriptor.Lifetime)); |
| | 10 | 25 | | } |
| | | 26 | | |
| | | 27 | | private static IConsoleLogProvider CreateProvider(IServiceProvider serviceProvider, ServiceDescriptor descriptor) |
| | | 28 | | { |
| | 9 | 29 | | if (descriptor.ImplementationInstance is IConsoleLogProvider instance) |
| | 4 | 30 | | return instance; |
| | | 31 | | |
| | 5 | 32 | | if (descriptor.ImplementationFactory != null) |
| | 5 | 33 | | return (IConsoleLogProvider)descriptor.ImplementationFactory(serviceProvider)!; |
| | | 34 | | |
| | 0 | 35 | | if (descriptor.ImplementationType != null) |
| | 0 | 36 | | return (IConsoleLogProvider)ActivatorUtilities.CreateInstance(serviceProvider, descriptor.ImplementationType |
| | | 37 | | |
| | 0 | 38 | | throw new InvalidOperationException("The console log provider registration is invalid."); |
| | | 39 | | } |
| | | 40 | | } |